Search Unity

simple collision question

Discussion in 'Scripting' started by eliphas, Jun 28, 2006.

  1. eliphas

    eliphas

    Joined:
    Mar 31, 2006
    Posts:
    9
    I started to feel like i knew what i was doing. I managed to get objects to move and react to each other, then this collision problem came along and stumped me! Any help would be greatly appreciated.

    My scene has a cannon which instantiates a sphere with a rigidbody and sphere collider attached. This sphere is used as a projectile.

    The scene also contains some targets, which are simple cubes with box colliders attached.

    The idea is for the projectile to bounce off everything until it hits a target, then other things should happen, but at the moment i just want to destroy the sphere instance.

    I've been through all the documentation and threads on the forum and have come up with something i think should work. Please can someone explain what i'm doing wrong.

    I have the following script attached to the target:

    Code (csharp):
    1.  
    2. var thingThatHits : GameObject;
    3.  
    4. function onCollisionEnter(other){
    5.  
    6.      if(other == thingThatHits)
    7.           {
    8.           Destroy(thingThatHits);
    9.           }
    10. }
    11.  
    I then drag the original projectile object onto thingThatHits in the inspector. This is sort of based on a piece of code i found in a thread, but that code had the equivalent of me writing:
    Code (csharp):
    1.  
    2. var thingThatHits : projectile;
    3.  
    But when i try this unity says projectile is not a valid type.

    Thanks for any help!
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    After a colon you indicate the type of the variable. Using GameObject should be ok. 'projectile' with a lowercase 'p' is probably not a type (e.g. if your script is called Projectile, the type is Projectile with an uppercase P)
     
  3. eliphas

    eliphas

    Joined:
    Mar 31, 2006
    Posts:
    9
    Sorry, i forgot to put in my original post, i've tried using the script which includes the thingThatHits : GameObject. But the projectile still bounces off the target.
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Did you write OnCollisionEnter with an uppercase 'O' (in your script it's lowercase)? You can check whether the function is called at all by doing something like
    Code (csharp):
    1. print("I'm called!");
    in there.
     
  5. eliphas

    eliphas

    Joined:
    Mar 31, 2006
    Posts:
    9
    I'm pretty sure i typed OnCollisionEnter, unfortunately i'm at work at the moment so don't have access to unity, but i copied the syntax from the documentation so i am assuming i used uppercase.

    Is there any reason that you can see why my script isn't working? Or can you think of a better way to do the same job?
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Comparing objects can be a tricky. By default comparing objects is done by checking if the two variables point to the same object instance. (Except for value types like Vector3, int, float and string where the value is compared instead.) You are comparing a Collision (other) to a GameObject (thingThatHits.) This can never be the same object.

    Try this instead (I changed so you compare the transform property of the Collision to a stored transform reference):
    Code (csharp):
    1.  
    2. var thingThatHits : GameObject;
    3.  
    4. function OnCollisionEnter(other){
    5.  
    6.      if(other.transform == thingThatHits.transform)
    7.      {
    8.           Destroy(thingThatHits);
    9.      }
    10. }
    11.  
     
  7. eliphas

    eliphas

    Joined:
    Mar 31, 2006
    Posts:
    9
    Thanks guys, it worked like a charm.
    And so onto the next challenge...