How to reference velocity?

Is there any API reference for velocity in Play Canvas? I’ve searched around and I tried both linearVelocity() and just velocity() but they don’t seem to work. for context I’m trying to make a headbob feature.

var enable = true;//turns head bob on or off
var updown = 100;//the intensity of the headbob



while (this.entity.linearVelocity().x != 0); {//checks if the player is stationary or moving before executing the headbob
    if (this.entity.getPosition().y <= 0.5) {//checks to see if the player is on the ground before executing the headbob
        this.entity.rigidbody.translate(0, updown, 0);//moves the player up, I don't have something to move the player down yet but I will probably make it so once it gets to a certain point, it will go down
    }
    
}

Hi @OMN1B,

Yes it’s a property of the rigid component, not the entity class:

// returns a Vec3
this.entity.rigidbody.linearVelocity;

https://developer.playcanvas.com/api/pc.RigidBodyComponent.html#linearVelocity

It’s not working, whenever I do that it gives me an error: “TypeError: Cannot read properties of undefined (reading ‘rigidbody’)”

Do you have a rigidbody attached to your entity?

Yes I do

Try sharing a sample project, since it’s not possible to debug further like this.

https://playcanvas.com/editor/scene/1527496
the code is in headBobController.js

Hello @OMN1B,

I’ve taken a look at your code. Firstly, I see you are attempting to call

this.entity.rigidbody.translate(0, updown, 0);

However, the rigidbody component does not have a translate method. You can either call translate directly from entity or use a similar method that is supported by the rigidbody component, like rigidbody.addForce

this.entity.translate(0, updown, 0); // OR
this.entity.rigidbody.applyForce(0, updown, 0);

Also, you check for rigidbody.linearVelocity() with the parenthesis, as if it was a function, but it is only a property, so the parenthesis is not necessary. Use rigidbody.linearVelocity instead.

this.entity.rigidbody.linearVelocity

Furthermore, I noticed you have placed your while loop outside the methods of the entity. Your loop must be within one of the defined methods or it will be called outside the expected scope and it will cause problems, e.g. not recognising the presence of the rigidbody component. Try doing this:

// initialize code called once per entity
HeadBobController.prototype.initialize = function () {
    while (this.entity.rigidbody.linearVelocity != 0); {//checks if the player is stationary or moving before executing the headbob
        if (this.entity.getPosition().y <= 0.5) {//checks to see if the player is on the ground before executing the headbob
            this.entity.rigidbody.translate(0, updown, 0);//moves the player up, I don't have something to move the player down yet but I will probably make it so once it gets to a certain point, it will go down
        }
    }
};

As an additional note, I would be very careful of using a while loop as it can easily crash or freeze your game should its if statement never be false.

As you seem quite new to scripting, I highly recommend you watch one of our crash course tutorials before going ahead. This will help you diagnose and fix your problems in your scripts.

1 Like