Search Unity

Mine - Landmine with script

Discussion in 'Scripting' started by Grune, Jun 13, 2010.

  1. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    I build a simple mine:

    The player (Object with the tag player) collides an it goes off, dealing some dammage

    Code (csharp):
    1.  
    2. var sound : AudioClip;
    3. var soundVolume : float = 2.0;
    4.  
    5. var explosion : Transform;
    6. var spawn : Transform;
    7.  
    8.  
    9. function OnTriggerEnter(other:Collider) {
    10.     if (other.tag != "Player")
    11.         return;                                         // return beendet hier die Funktion         fals das Tag nicht Player
    12.    
    13.     if (sound)                                          // ansonsten gehts hier weiter
    14.         AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
    15.    
    16.     Destroy(gameObject);
    17.    
    18.     if (explosion)
    19.     Instantiate (explosion, transform.position, transform.rotation);
    20.    
    21.     if (spawn)
    22.     Instantiate (spawn, transform.position, transform.rotation);
    23.        
    24. }
    25.  
    26.  
    It plays a soundfile, kills itself, spawns some debris and an explosion which does the damage

    One problem is: now it uses it's collider as trigger but i want it to be a riggidbody to be able to bounce of the floor. And at the moment you can't shoot it. It deals but does not receive dammage.
     
  2. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    Simply change:

    function OnTriggerEnter(other:Collider)

    to

    function OnCollisionEnter(other:Collider)

    ...then you can use it as any rigid body.
     
  3. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    you should look at the damage receiver script in the FPS-Demo. Its a good script to control damage. :D
     
  4. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    thanks alot. It makes sense to me that it should react to a collision and not via a trigger.

    But now how do i make it distinguish to which tag it reacts. Before i made it blow only with player tagged objects

    Code (csharp):
    1. function OnCollisionEnter(collision : Collision) {
    2.     if (collision.tag != "Player")
    3.         return;    
    This part does not work anymore: if (collision.tag != "Player")
     
  5. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    I did look at the fps tutorial and came up wit a not very graceful solution:

    I use 2 parts the Mine and the Trigger




    The Mine is a RiggidBody

    Code (csharp):
    1. var explosion : Transform;
    2. var spawn : Transform;
    3.  
    4.  
    5.  
    6. function BlowUp () {
    7.     print ("BOOOM!!!");
    8.    
    9.     Destroy(gameObject);
    10.    
    11.     if (explosion)
    12.     Instantiate (explosion, transform.position, transform.rotation);
    13.    
    14.     if (spawn)
    15.     Instantiate (spawn, transform.position, transform.rotation);
    16.        
    17. }
    The red part on top is the trigger it's collider is set to trigger ( seems they should not touch each other)

    Code (csharp):
    1. var sound : AudioClip;
    2. var soundVolume : float = 2.0;
    3.  
    4. var target : Transform;
    5.  
    6.  
    7.  
    8. function OnTriggerEnter(other:Collider) {
    9.     if (other.tag != "Player")
    10.         return;                                         // return beendet hier die Funktion         fals das Tag nicht Player
    11.    
    12.     if (sound)                                         
    13.         AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
    14.    
    15.     print ("BUM");
    16.    
    17.     if (target.GetComponent("MineV2") != null)         
    18.         target.GetComponent("MineV2").BlowUp();
    19.  
    20. }
    21.  
    22.  
    feel like a workarount to me but does the job
    But i am always interested in better solutions. :wink: