Collision detection

i am having trouble having a entity not go through a asset i want it to act like a solid structure what should i do

Hallo! Maybe this tutorial will help you get started:

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

thanks

1 Like

i looked at a web page from play canvas that told me to right this code to test if a object has a Collision with on or the other and nothing happens here is the code i written is it right

var Collider = pc.createScript('collider');

// initialize code called once per entity
Collider.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Collider.prototype.onCollisionStart = function (result) {
    if (result.other.rigidbody) {
        this.entity.sound.play("slot 1");
    }
};

So do you want the code to run only when the entity starts colliding with a rigidbody, or do you want it to constantly play a sound while it’s colliding.

i want it to constantly play a sound while it’s colliding

Try adding a tag to the entity your checking the collision for. For example, let’s say the tag is entity, then you would put:

if (result.other.tags.has('entity')) {
    this.entity.sound.play("slot 1");
}

thank you

Then try this:

var Collider = pc.createScript('collider');

// initialize code called once per entity
Collider.prototype.initialize = function () {
    this.entity.collision.on('contact', this.onContact this);
};

Collider.prototype.onContact = function (result) {
    if (result.other.tags.has('entity')) {
        this.entity.sound.play("slot 1");
    }
};

Make sure the entity your checking collision with has the tag that’s being checked within the collision function. (In this case, entity).