[SOLVED] Why doesn't this work?

Hello, all!
I have been using colision triggers with dynamic rigidmody movement code and cant seem to use

var Planet = pc.createScript('planet');
Planet.attributes.add('alphacam',{
    type:"entity",
})
// initialize code called once per entity
Planet.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.entity.collision.on('collisionend', this.onCollisionEnd, this);
this.alphacam.enabled = false;
};

Planet.prototype.onTriggerEnter = function(result) {

};
Planet.prototype.onCollisionStart = function (result) {
    if (result.other.tags.has('a')) {
this.alphacam.enabled = true;
this.entity.enabled = false;
    };
};

When the entity comes in contact with the collision with a tag called ‘a’, the alphacam is supposed to enable. This does not happen. Here is a vid of how to get to everything. The class doesn’t matter. and you shouldn’t faze through the sphere, there is a collision that has the tag ‘a’ look in the hierarchy for alpha then sphere.

Hi @Codeknight999!

Your script looks correct to me, except line 19 because you have to close a statement with only a } sign. I’m not sure if this can cause the problem.

How do you move the entity?

Please note both entities need a collision and rigidbody component to fire the oncollisionstart event. Otherwise you need to consider the ontriggerenter event on the entity without a rigidbody component.

I took off the ; and nothing happened.

I am using translateLocal. Does that affect anything? I am also using a static rigid body.

@Albertos do you know anything about why this isn’t working?

Because a dynamic rigidbody need to moved by forces. When you use translateLocal() you move the entity itself, but the rigidbody is not moved correctly with it. That’s why there no collision on expected moments anymore.

Is there a way to move the rigidbody as well?

Yes, you need to apply a force on the rigidbody instead of moving the entity. Alternatively you can make the rigidbody kinematic instead of dynamic, but that means it will not stopped by obstacles.

I switched the rigid body to kinematic and that seemed to fix it. Thanks for your help! This was a big problem.

1 Like