Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

target rigidbody collision

Discussion in 'Scripting' started by nmceri, Aug 12, 2005.

  1. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    I've got a bullet object that i'd like to have destroy another object with something like this.

    Code (csharp):
    1. function OnCollisionEnter() {
    2.    var target : the object hit;
    3.    Destroy (target, 0.0);
    4.    Destroy (gameObject, 0.0);
    5. }
    I'd like to know how to target the object i've just hit.

    On a related note. How can i ignore collisions with a specific object. I'm instantiating bullets from multiple sources and sometimes they will cross. I'd like it if they didn't blow eachother up!
     
  2. dan

    dan

    Joined:
    Jul 22, 2005
    Posts:
    151
    Put this on the target instead of the bullet:

    Code (csharp):
    1. var bullet: the bullet;
    2.  
    3. function OnCollisionEnter( other ) {
    4.    
    5. if (other == bullet) {
    6. //destory stuff
    7. }
    8. }
     
  3. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    This is a perfect solution. Thank you for the help.
     
  4. dan

    dan

    Joined:
    Jul 22, 2005
    Posts:
    151
    No problem. I was scratching my head about this one the other night.

    :)
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    .... that doesn't quite work like expected. The parameter to OnCollisionEnter is a Collision object.

    You can fetch the other object like this:

    Code (csharp):
    1.  
    2. function OnCollisionEnter( hit ) {
    3.    var other = hit.other;
    4.    // *** do stuff ***
    5. }
    6.  
    I would let the other object handle the damage and self-destruction itself using messages. So on the objet to destroy, I would add a script containing the following.

    Code (csharp):
    1.  
    2. // Damage handling code
    3. var hitPoints = 10;
    4. function ApplyDamage( damage ) {
    5.    hitPoints -= damage;
    6.    if( hitPoints <= 0 ) {
    7.      Destroy(gameObject, 0.0);
    8.    }
    9. }
    10.  
    And then in the bullet code:
    Code (csharp):
    1.  
    2. var damage = 100; // instant kill :-P
    3. function OnCollisionEnter ( collisionInfo ) {
    4.     collisionInfo.other.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver );
    5.     Destroy(gameObject, 0.0);
    6. }
    7.  
    This will promote better encapsulation, and has the added bonus that if you don't attach the Apply Damage script to an object, it won't get destroyed.
     
  6. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    Thank you for the great example. I hope the widgets this competition spawns will be a great resource for other beginners :)
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    No problem...

    I just edited my previous post and added a SendMessageOptions.DontRequireReceiver to the SendMessage call. If that one wasn't set, Unity would print an error if the bullet hit an object without an ApplyDamage function.
     
  8. dan

    dan

    Joined:
    Jul 22, 2005
    Posts:
    151
    Yeah, that does look like a better solution, Freyr.

    I'm at work and couldn't actually test what I was typing.

    On a side note, how sad is it to work at a Sex Research Institue, and be browsing these forums instead of working?
     
  9. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    i get an 'expecting COLON, found ';" error while trying the damage handling script. Did i miss something?
     
  10. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Where to go with this ... :wink:
     
  11. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    DOH!...Should have tested it before posting... This is a bug in our Javascript implementation...the <= and >= operators aren't recognised...

    Code (csharp):
    1.  
    2. var hitPoints = 10;
    3. function ApplyDamage( damage ) {
    4.    hitPoints -= damage;
    5.    if(  hitPoints < 1 ) {
    6.      Destroy(gameObject, 0.0 );
    7.    }
    8. }
    9.  
    10.  
     
  12. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
     
  13. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    while trying to implement this code as is I get an error when the bullet hits the object.
    "Failed to SendMessage "AppyDamage" to class "x""
    x being the name of the script on the hit object.
     
  14. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Did you remember to add the SendMessageOptions.DontRequireReceiver parameter to the SendMessage call?

    And did you make sure that the message name and the function in the receiving object have the same name?
     
  15. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    Yes to both of those. It's odd but after getting that error the first time I simply thought my custom implementation of your code might be a bit off. So then I tried a simple cut and paste of your code to double check if that was working, and i get the same error.

    your first code snippet works like a charm though.
     
  16. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Weird... I just tested it myself and it works for me.

    Here are the scripts I used in the test project. The DamageEnflicter should be attached to the bullet and the DamageHandler to the target.
     

    Attached Files:

  17. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    I've placed your scripts into my project but have received the same results.
    If you have some time I've uploaded the project here.
    http://www.cybercom.net/~erin/unity/widget.zip

    Viewers of this project feel free to make suggestions on my implementation of features. It's all very much hacked together sofar but I feel it's making some nice progress.
     
  18. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    I feel like i've run into a wall here. I've tried making 2 new documents using only the scripts you've uploaded with the default red boxes and a script to move the projectile forward, just to see if there was something strange about my project,but i'm still recieving the error when they come in contact.

    I don't know enough about Unity to really know what else i should try, as apparently it works perfectly for you. Can anyone suggest more ways i can trouble shoot this?
     
  19. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  20. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    Thank you Joachim. I was still using beta 1. I just tried it in beta 2 and it works exactly as described.
    I had been holding off from upgrading becase i didn't want any unforseen difficulties getting in the way of completing my widget, but ultimately not upgrading did just that!