[SOLVED] Enemies and walls and finish line trigger not working

Please help, this is due soon and I already sent it to a friend!

So basically ever since I have had a rigid body component on the player entity, which is necessary for the movement script, It makes it so that the finish line triggers and everything else does not work. Is there a way around this? Need help asap.
Here is the project:
https://playcanvas.com/project/867647/overview/projectforschool

I will not be able to reply for a few hours, but I will try my best to get back and fix it as soon as I can. Thank you Playcanvas for the support you give me all the time on my projects.

Your player entity has a static rigidbody component. This is not possible, because the player is a moving entity. If you move the player with forces you have to use a dynamic rigidbody, otherwise you have to use a kinematic rigidbody.

In your trigger script you have to check if the entity that triggers the trigger is the player entity.

Trigger.prototype.onTriggerEnter = function(entity) {
    if (entity.name === 'Player') {
        console.log(entity);
        entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
        entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
        entity.rigidbody.teleport(0, 0, 17);
    }
};
1 Like

Oh thank you so much, sorry it took a long time for me to respond. I am still new to this rigidbody stuff but I found a helpful article that I will read thoroughly. Thank you so much.

Hey, so I have got the rigidbody changed and applied the change to the Trigger.js script, however it is still not working. I have a collision on the finish line trigger and rigidbody type static. I do not know if that is an issue, and believe it is not because I have rigidbody type static on the ground, and it collides with the player just fine. Do you know why this is happening?

The trigger entities may not have a rigidbody component. You need to remove these or change your script with the onCollisionStart event.

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

1 Like

Do you mean like this?

var Trigger = pc.createScript('trigger');


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

//this code is the code not working, supposed to execute when something collides with this entity.
Trigger.prototype.onCollisionStart = function(entity) {
    if (entity.name === 'Player') {
        console.log(entity);
        entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
        entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
        entity.rigidbody.teleport(0, 0, 17);
    }
};

Trigger.prototype.update = function(dt) {
    var entity = this.app.root.findByName('Player');
};


//use entity.getPosition()
//then teleport the entity using entity.setPosition and use math with the parameters

I have probably made another mistake in the code. I am doing something wrong here because it does not seem to be working. (teleporting).

Change this line to:

this.entity.collision.on('collisionstart', this.onCollisionStart, this);
1 Like

I have updated the code, but it is still not working/teleporting. Is there anything I should know about the components that I have assigned to it, and if this is causing the issue? My project is here, just in case you need it.

First problem that I see is that your Player entity has no collision component.

1 Like

Now that I fixed that silly mistake, it seems to be working. However, now that gravity is applied it is super laggy. Can I turn gravity off via script?

You can try to use a kinematic rigidbody instead of a dynamic rigidbody.

1 Like

Thanks. It is still really laggy but if this is all i can do to reduce the lag, then that’s okay. Thanks for all of this help. I really appreciate it.

Oh, weird. Everything but the finish line trigger is working. I’ll sleep on it, and fix it in the morning. Thanks for all of the help, though, you really took a lot of weight off my chest. I appreciate it!

You’re welcome! I will check your project on my laptop tomorrow to see what can cause the problems. :ok_hand:

Hi @CRAYPHiSHQUESTIONS! I debugged your project.

The reason for this is the mesh collision that you use on your second Render entity of your Player entity. You can remove that collision component because there is no reason to use it.

This is because the result of onCollisionStart works a little different than the result of onTriggerEnter. I often forget that myself. Below the correct way, with using other.

Trigger.prototype.onCollisionStart = function(entity) {
    if (entity.other.name === 'Player') {
        entity.other.rigidbody.linearVelocity = pc.Vec3.ZERO;
        entity.other.rigidbody.angularVelocity = pc.Vec3.ZERO;
        entity.other.rigidbody.teleport(0, 0, 17);
    }
};

Thanks so much, I really appreciate the help. Once again, Playcanvas has helped me solve the problem! In the future I will try to stay away from using mesh collisions with models that have a lot of vertices. Thank you!

1 Like