Collision events between rigidbody entities and collision-only entities

I have a Player entity and an enemy Cactoro entity in my scene. The enemy only has a collision component so that I can move through it as the player and many enemies can move through each other. The player has a rigidbody component (and a collision component). I want to have collision events between the player and the enemy.

To have two entities with only a collision component, the player also has a child entity with that setup. Neither the player or the child component (PlayerAwareness) register the events.

Editor: PlayCanvas | HTML5 Game Engine
Checkpoint: b39d07d
Discord thread: Discord

@Impostrophe I forked your project and made your PlayerAwareness script a little more basic so it will help your understanding

var PlayerAwareness = pc.createScript('playerAwareness');

/** Attributes */
PlayerAwareness.attributes.add('playerRef', { type: 'entity' });

// initialize code called once per entity
PlayerAwareness.prototype.initialize = function() {

    this.entity.collision.on('triggerenter', this._onTriggerEnter, this);

};

// Overlap Events
PlayerAwareness.prototype._onTriggerEnter = function(entity) {

    if(entity.tags.has('enemy')) {

        console.log("Enemy Encountered");
           
    }    
};

I don’t know completely from your description all the in and outs of what you are trying to achieve. What this script will do is print out to the console every time your collision enters the enemy area. In the inspector you will see it count when you bump into them. I did notice that your collision entity on your player is very big. So if you want to see it repeat you must back up some and then bounce into again. Hope this helps.

1 Like