[SOLVED] Trying to execute code on collision

Hello, I am trying to execute the log() function onTriggerEnter. So when something collides with the entity this script is set to, it executes a message saying that it has detected a collision via console.

here is my code:

//this code is the code not working, supposed to execute when something collides with this entity.
Trigger.prototype.onTriggerEnter = function(entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // display a message after collision via console.
    console.log('I have detected a collision!');

I do have everything defined/set, and collision is on, as this is the beginning of my code:

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);

I do not know if I need to apply this to the front of any of the functions, but I would appreciate help!

Hi @CRAYPHiSHQUESTIONS! As far as I can see your code seems to be correct. Maybe you can also check the things below.

  • Is the script attached to the trigger entity?
  • Has the trigger entity a collision component?
  • Has the other entity a collision component?
  • Has the other entity a rigidbody component?
1 Like

Oh my other entity did not have rigidbody! Thank you so much! It is working!

1 Like