How to detect which object was collided by the main object?

As far as I know, I must have some objects with rigid body’s and collisions but, can someone help me in this?

I’ve “n” objects with animations (actually 6 objects) and I want to detect each collision, but I added the collision script to my main object (which I’m controlling).

I tried this in my main object:

var Colliders = pc.createScript('colliders');

// initialize code called once per entity
Colliders.prototype.initialize = function() {
   // Each of this objects has a collision and rigid body 
    this.entity.collision.on('collisionstart', this.onCollision, this);
    this.model1 = this.app.root.findByName('Man');
    this.model2 = this.app.root.findByName('Man02');
    this.model3 = this.app.root.findByName('Man03');
    this.model4 = this.app.root.findByName('Man04');
    this.model5 = this.app.root.findByName('Man05');
    this.model6 = this.app.root.findByName('Man06');
 
};

Colliders.prototype.onCollision = function (result) {
    console.log(result.other.name);
};

But at the moment that my main object collided with any object, the console log is not printed and if I add this collision script to each object instead of in my main object, the console log is printed. Sorry for my misspelling.

Hi @Axel_Saucedo,

Take a look at Collision and Triggers tutorial on how to start detecting collisions between your objects.

It explains the difference between triggers and rigid bodies, for detecting collisions and provides starting code.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Your collision script seems correct, to be attached to your main object, most likely the issue is somewhere on your scene setup.

If you are unable to resolve this, try posting a working scene example to take a look.

1 Like

What kind of animations are these? The rigidbodies/collision volumes won’t move with the animation.

They are static for now due to I haven’t complete the entire animation yet, they’re walking but in the same coordinates

https://playcanvas.com/editor/code/674858?tabs=29890446
this may turn out to be useful, it’s a script that i use on arrow

1 Like