OnCollisionEnter event not called issue

Hi guys,

in my game, i have a player, who can shoot spheres and randomly spawning cubes. When I shoot a cube with a sphere, i want the sphere to add 10 points to the player’s score (stored in the script ‘PlayerHealth’) an then destroy itself.

Here is what the game looks like:

This is the PlayerShooting script:

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

public class PlayerShooting : NetworkBehaviour
{
    public Transform muzzleRight;
    public Transform muzzleLeft;
    public GameObject bulletPrefab;

    private void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            CmdFireLeft();
        }
        if (Input.GetMouseButtonDown(1))
        {
            CmdFireRight();
        }
    }

    [Command]
    void CmdFireLeft()
    {
        var bullet = (GameObject)Instantiate(bulletPrefab, muzzleLeft.position, muzzleLeft.rotation);
        bullet.GetComponent<Bullet>().playerOrigin = this.gameObject;
        NetworkServer.Spawn(bullet);
    }

    [Command]
    void CmdFireRight()
    {
        var bullet = (GameObject)Instantiate(bulletPrefab, muzzleRight.position, muzzleRight.rotation);
        bullet.GetComponent<Bullet>().playerOrigin = this.gameObject;
        NetworkServer.Spawn(bullet);
    }
}

This is the Bullet script:

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

public class Bullet : NetworkBehaviour
{
    public float bulletSpeed = 0.8f;
    private float lifetime = 10f;
    [SyncVar] public GameObject playerOrigin;

    private void Update()
    {
        transform.Translate(0, bulletSpeed, 0, Space.Self);
        Destroy(this.gameObject, lifetime);
    }

    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("yo");
        if (collision.collider.tag == "Player")
        {
            var hitPlayer = collision.collider.gameObject;
            var health = hitPlayer.GetComponent<PlayerHealth>();
            if (health != null)
            {
                health.TakeDamage(10);
            }
            Destroy(this.gameObject);
        }

        if (collision.collider.tag == "Shootable")
        {
            playerOrigin.GetComponent<PlayerHealth>().score += 10;
            Destroy(this.gameObject);
        }
    }
}

That’s also where the collision of the bullet and the cube (with the tag ‘Shootable’) should be detected. But i dont know anymore what could be wrong.
The bullet is just a Unity sphere 3Dgameobject with a sphere collider with default settings and the cube is a unity cube with its default box collider and a rigidbody with constraints freezed, no gravity, non-kinematic and discrete collision detection.
Actually it is not only that the: ‘if collision tag != shootable’ part is not run, but the whole OnCollisionEnter event is not triggered and thus the Debug.Log(“yo”) is not displayed.

However it is triggered somehow, when i shoot multiple bullets. So i figured that, when there are at least 2 spheres inside a cube, the collision thing will be executed perfectly, but only for the second bullet, not the first one. So when there are like 10 bullets inside, 9 bullets will destroy themselves, and 1 bullet (which was fired first) will make its way out of the cube, ‘ignoring’ the collision detection. Not sure if this is rather helpful for you or more irritating, but well…

Thank you very much for reading all this stuff, and i’d be very happy for an answer!

-Jonas

Take a look at the bottom of this page in the manual - Unity - Manual: Introduction to collision.

Does the bullet have a rigidbody on it?

2 Likes

Thanks for your answer! That was really the problem. The bullet didn’t have a rigidbody, but i thought i should work because the cube had one. It was just strange because in some moments it would trigger collision as i said, but however now it works. Thx!