OnCollisonEnter method not triggering when collision happens

I have a player object, and a “Shooter” object that creates instances of a bullet, and that bullet moves toward the player once it is instantiated. The bullet’s update method tracks the player’s position and then moves the bullet toward the direction of the player.

I wanted to use OnCollisionEnter so that when the bullet hits the player it gets destroyed,

The problem is that the actual collision works, but it does not trigger the OnCollisionEnter in the script that is on each instance of the bullet object.

To fix this I tried using OnTriggerEnter instead and setting only the bullet instance to be a trigger, however, this got rid of any collision between the bullet instance and the player object.

To be sure the problem wasn’t because of the player object I put a sphere that had a rigidbody in front of the path that the bullets flew in, and the bullets bounced off the sphere but then continued towards the player, without triggering OnCollisionEnter.

Am I using the OnCollisionEnter method wrong because during any collision the bullet had I didn’t see the debug.log I put in it.

Both the bullet instance and anything it was hitting had a Rigidbody.

Code for the Shooter object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooter : MonoBehaviour
{

    public GameObject addBullet;
    private GameObject player;
    private float lastTimeShooted = 0;
    private int shoot;

    // Start is called before the first frame update
    void Start()
    {
        this.player = GameObject.FindGameObjectWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        shoot = Random.Range(3, 15);

        if((lastTimeShooted + shoot) < Time.time) {
            GameObject newInstance = Instantiate(addBullet) as GameObject;
            newInstance.transform.position = this.transform.position;
            // Bullet bullet = newInstance.GetComponent<Bullet>();
            // newInstance.Target = this.player;
            lastTimeShooted = Time.time;
        }
    }
}

Code for the bullet script on each bullet instance:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{

    private float Speed = 3.5f;
    public GameObject Target = null;

    // Start is called before the first frame update

   

    void Start()
    {
                Target = GameObject.FindGameObjectWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        if (Target != null) {
            Debug.Log("Shooting");
            float step = Random.Range(1,5) * Time.deltaTime;
            this.transform.position = Vector3.MoveTowards(this.transform.position, Target.transform.position + new Vector3(0,0.5f,0), step);
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collision Works");
       
         GameObject.Destroy(this.gameObject);
    }
  


}

Any help would be greatly appreciated!

I just briefly glanced at this, and im not sure if related or not, but i noticed you mention a rigidbody. In my experiences, movement and managing a rigidbody should be within a FixedUpdate, so that the physics calcs can actually work correctly. In the update, i have noticed sometimes it just doesnt work out well. But im not a guru of this stuffs. Someone else will most likely have better advice for you.

1 Like

Thank you for the suggestion, I tried using a FixedUpdate instead of a regular update but I received the same result. The physics of the collision still work but a collision doesn’t trigger the OnCollisionEnter method.

Here is a video of the issue:

The collision is working but when a sphere collides with this skeleton it should be deleted, or at least the Debug.Log would trigger, but it doesn’t.

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

https://discussions.unity.com/t/866410/5

https://discussions.unity.com/t/878046/8