[SOLVED] How to check if an entity is currently moving

Hi @RoaringLegend456,

Does your entity has a rigid body?

If that’s the case you can check if it’s linear velocity is zero or close to zero to if it rests.

const isStopped = this.entity.rigidbody.linearVelocity.length() < 0.01;

Thank You it works

Nevermind it doesn’t work the way I want it to

It checks the velocity in every direction when I only want 1 direction to be checked. I tried putting .y at the end but it didn’t work

Check here:

I removed the .length but it still doesn’t work

here is my code

if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if(this.entity.rigidbody.linearVelocity.y == 0.0){

Hi @RoaringLegend456! I just learned the way below from @Leonidas. I didn’t test it yet, but you can give it a try.

var velocityOnY = Math.abs(this.entity.rigidbody.linearVelocity.y);
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
    if (velocityOnY === 0) {

1 Like

I tested it but it still doesn’t work. thankyou for helping

I figured it out.
I am guessing when the entity moves along the z & x axis the entity bounces a little on the y-axis so I fixed this by taking the velocityOnY from = to 0 too < 0.1

if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if(Math.abs(this.entity.rigidbody.linearVelocity.y) < 0.1){

Yes indeed, I see that the velocity is never exactly 0 even when you are not moving. (Also applyImpulse is not working for me if the linearVelocity is set to 1).

try chang wasPressed to isPressed

if (app.keyboard.isPressed(pc.KEY_SPACE)) {
        if(Math.abs(this.entity.rigidbody.linearVelocity.y) < 0.1){

let me know if that works

It’s working when the linearVelocity is not set to 1 in the editor. Thank you!

the difference between isPressed and wasPressed is that wasPressed will only send 1 update when the button is press while isPressed will keep sending updates as long as the button is pressed

That’s right, but in this case it doesn’t matter, because the extra velocity check prevents you from jumping repeatedly.

not necessarily that I found out for a short period of time at the peak of the jump the velocity is less than 0.1 so if you click at the right time which is hard you can jump again

you can find this out with the ispressed variant and press and hold the jump button

In that case there is even more reason to use wasPressed instead of isPressed I guess?

since it is still doable with the wasPressed I am trying to figure out what the best way to make sure you can’t jump again at that small point of time

I wonder if there is a positive and negative velocity?

If that’s the case you also have to check if the velocity is higher than -0.1.

there is but Math.abs gets rid of the negative velocity and replaces it with a positive velocity

this is a picture where y is the velocity after it went through the Math.abs function

1 Like

Ah, good to know. Then why you can jump when you are in the air?