[SOLVED] Move speed of entity


I have discovered that the move speed of this.entity.translateLocal (0,0, -0.01) is not always the same. Sometimes I have to increase the speed slightly to get the same result in the game.

I tried to use times delta time but that results in unexpected and unstable movements of my kinematic entity.

Someone a solution?

With kinematic entities, they should be moved by changing the velocities of the rigid body. Moving them by changing the entity position can cause unexpected behaviour.

Thanks for your reply Yaustar! I try to do this with this.entity.rigidbody.applyForce(0, 0, -10) but then my entity is not moving. So how do i change to velocities correctly?

You can change the linear velocity directly: https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#linearVelocity

Oke, i tried:

this.entity.rigidbody.linearVelocity.z = -1;


var p = this.entity.getPosition();
this.entity.setPosition(p.x, p.y, p.z -0.05);

but in both cases the enity is not moving forward (in the direction of my character)…

It’s one of those cases you have to set the vec3 to apply unfortunately.

    var vel = this.entity.rigidbody.linearVelocity;
    vel.z = 0.1;
    this.entity.rigidbody.linearVelocity = vel;

Sorry, i don’t understand your reaction. My entity is moving with this solution, but not in the local rotation of the entity. So i can’t use it.

With this.entity.translateLocal(0,0,-this.moveSpeed) everything works well, but when this.moveSpeed is 0.01 sometimes (i think when CPU or connection is busy) in game it feels like 0.008. And because of that little diffrent the game doesn’t play well anymore.

Ah, if you need to move it along the local axis, just grab the axis you need (e.g forward, right, up) and multiply it by the speed.

eg: https://playcanvas.com/editor/scene/838809

    var vel = new pc.Vec3().copy(this.entity.forward).scale(this.speed);
    this.entity.rigidbody.linearVelocity = vel;

Second example using your method of translateLocal (which I’m surprised works) and dt: https://playcanvas.com/project/652314/overview/kinematic-example-ii

It seems that both options work. I no longer see unexpected movements when i use delta time, so I will first apply this option to see if this solved the problem. Thanks for your help!