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.