onCollisionEnter Never Called

Hi guys, I installed Cocos Creator 3.8.4 yesterday and encountered a very confusing problem. I have a little experience with Unity but I’m new to Cocos.

I created an empty 3D project and tried to make a shooter game.
Bullets are created on a shoot point, traveling to the front of the shoot point.

The bullets have a cylinder collider and a rigibody attached to each, where useGravity is off and the type is dynamic. isTrigger for the collider is also off.

The enemy unit has capsule collider but not a rigidbody.

While testing on a browser, I can visually see the bullets bounced sideways by the enemy. However, the onCollisionEnter(other: Collider, self: Collider) method in the Bullet.ts script attached to the bullet prefab is never called.

What could I have missed?

post your code here, you might have overlooked something.

Please check the /wooway777/RunnerTest repository on Github

No, it’d be helpful if you post the code which evaluates the collision here, and provide the url.

The forum doesn’t allow posting url. As a new user I cannot upload a package either.

import { _decorator, Component, Collider, RigidBody, PhysicsSystem } from 'cc';
import Projectile from './Projectile';
import Unit from '../Units/Unit';

const { ccclass, property } = _decorator;

@ccclass
export default class Bullet extends Projectile {
    @property
    override damage: number = 10; // Overriding inherited damage property

    private rigidbody: RigidBody | null = null;

    constructor(rigidBody: RigidBody) {
        super();
        this.rigidbody = rigidBody;
    }

    start() {
        this.rigidbody = this.node.getComponent(RigidBody);

        if (this.rigidbody) {
            // If the collision detection property is unavailable, adjust the behavior directly
            const physicsSystem = PhysicsSystem.instance;

            if (physicsSystem) {
                console.log("Using default physics system for collision detection.");
                // Attempt to set up the continuous collision detection here
                // You might also try adjusting other physics properties for performance
            } else {
                console.error("PhysicsSystem not found!");
            }
        } else {
            console.error("RigidBody component not found on Bullet!");
        }
    }

    update(dt: number): void {
        super.update(dt);
    }

    onCollisionEnter(other: Collider, self: Collider) {
        console.log("Collision detected between", self.node.name, "and", other.node.name);
        if (other.node) {
            console.log('Projectile hit:', other.node.name);
            other.node.getComponent(Unit)?.takeDamage(this.damage);
        }
        this.destroyProjectile();
    }

    destroyProjectile() {
        this.node.destroy();
    }

    private checkCollisionDetection() {
        if (this.rigidbody) {
            // If collisionDetection is not available, log other information
            console.log("RigidBody:", this.rigidbody);
        } else {
            console.log("RigidBody is not assigned!");
        }
    }
}


In a very simple and clean setup, the capsule falls onto the cylinder. onCollisionEnter is still not called…

import { _decorator, Component, physics, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CollisionDetection')
export class CollisionDetection extends Component {
    start() {
        console.log("Detector created");

        // Add RigidBody and Collider components if not already added
        const rigidBody = this.node.getComponent(physics.RigidBody);
        // rigidBody.type = physics.RigidBodyType.DYNAMIC; // Set RigidBody type
        rigidBody.collisionDetection = true; // Enable collision detection

        // No need to manually register the collision callback; Cocos Creator handles it automatically
        // rigidBody.on('onCollisionEnter', this.onCollisionEnter, this);
    }

    onCollisionEnter(other: physics.Collider, self: physics.Collider): void {
        console.log("Collision Detected: " + self.node.name + " and " + other.node.name);
    }
}

if it’s a bullet type, with high speed, you could try enabling continuous collision detection
/creator/3.8/manual/en/physics/physics-ccd.html
I am fairly new to cocos and haven’t worked with physics yet. I only know of unity. But I hope someone will help you out.

Thanks. I think it’s not about being continuous or not as I tried it before posting.

Also in the second test, I created a brand new project with nothing but the two items to collide and a script with onCollisionEnter only, and it didn’t work either.

did you make sure the script is attached as a component? edit: Oh I see, it might be a good idea to check out Cyan’s video tutorial on youtube to get a refresher on how to do it.