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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnCollisionEnter acting up

Discussion in 'Scripting' started by jorn818, Sep 28, 2016.

  1. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    so I got a soldier with a rifle, this rifle shoots a bullet (non-kinematic, with rigidbody) this bullet has also a trail renderer. I made a script that once the terrorist is hit by the bullet he gets destroyed, however when I hit him ingame nothing happens! (im using prefabshooting with a speed of 200); the bullet does not go through walls etc. so its not the Physics.
    the bullet upon impact of anything gets destroyed within 0.3 seconds.
    the Terrorist is tagged ''Terrorist''

    Code (csharp):
    1.  #pragma strict
    2.  
    3.  
    4. function OnCollisionEnter (hit : Collider){
    5.     if(hit.gameObject.tag == "Terrorist"){
    6.         Destroy(hit.gameObject);
    7.     }
    8. }
    9.  
    10. [code]
    upload_2016-9-28_22-4-4.png
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I believe making things non kinematic, in addition to making them ignore physics, all tells them to only fire the folllowing events:
    • OnTriggerEnter
    • OnTriggerExit
    • OnTrigger
    These are just like the OnCollision triggers but are only for non-kinematic objects. So you can separate Physics collisions from Non Physics collisions

    Is trigger may also have to be checkmarked.
     
  3. AYFukushima

    AYFukushima

    Joined:
    Feb 4, 2013
    Posts:
    12
    Hi!

    Don´t know if you tried, but are you sure the bullet is hitting the terrorist first (not some other collider)?

    Try to put a Debug.Log inside the "if" and outside, printing the object hitted.
     
  4. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97

    Well its hitting the Terrorists Player Controller (see picture on main post) maybe the player controller does not count as a collider?
     
  5. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97

    thanks for your time and help trying to find out my problem, the problem at the end was that it had to be

    (hit : Collision)
    and not
    (hit : Collider)
     
  6. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97

    Thanks for your help, I have found my answer
    it had to be

    (hit : Collision)
    not
    (hit : Collider)