Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnCollisionEnter() dosen't work if one object dosen't move??

Discussion in 'Scripting' started by BDCJR, Oct 21, 2013.

  1. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    Maybe someone can explain me about the problem I encountered.
    I have an object (arrow) that has a rigidbody, collider box and a script that contain OnCollisionEnter function and the other object (gate) only rigidbody and box collider. If both objects are moving, arrow feels the collision, otherwise not. I'm wrong somewhere or should I use another function?

    Code (csharp):
    1.    
    2. public virtual void OnCollisionEnter(Collision collision)
    3.     {          
    4.         string _tag = collision.gameObject.tag;
    5.         Debug.Log("hit: " + _tag);
    6.         if(_tag == "Tower" || _tag == "Ammo")
    7.             return;    
    8.         Debug.Log("is alive");
    9.         if(_tag == "Creep"  collision.gameObject.GetComponent<TDCreep>().isAlive)
    10.         {
    11.             collision.gameObject.GetComponent<TDCreep>().Hit(damage);
    12.            
    13.         }
    14.         else
    15.         {
    16.             Debug.Log(collision.gameObject.name);
    17.             Debug.Break();         
    18.         }  
    19.        
    20.         poolAmmo.Add(this.transform);  
    21.         poolEff.Add(Instantiate(explodeEff, collision.contacts[0].point, transform.rotation) as Transform, true);
    22.         isVisible = false;
    23.         isHitting = true;
    24.     }
     
  2. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    I found the problem. I had another object in the ray. :D