is there a tutorial for jumping on third person?
Hi @Unavailable,
So, you can take the default third person controller example from the Tutorials page and add jumping by binding a keyboard key (e.g. KEY_SPACE) to a method that applies an impulse upward (applyImpulse).
Search for jumping in the forum you will find a lot of similar posts.
Two things you will have to take care:
- Add a jumping animation that starts at the same time
- Play the animation once without looping it until the character falls back to the floor or on another object (check for collision).
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
if (this.jumping.state === false) {
this.entity.rigidbody.applyImpulse(0, this.jumpPower, 0);
this.jumping.state = true;
}
} else if (this.jumping.state === true) {
if (this._checkBelow() !== null) {
this.jumping.state = false;
}
}
};
PlayerMovement.prototype._checkBelow = function() {
return this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastPlayerBase.getPosition());
};
I’ve added this but can only jump 1 times
So, try debugging your _checkBelow method, what does it return and when does it run?
I think what you are looking for isn’t if there is a floor below the character (raycasts have infinite length) but if you are touching it. What I would do in that method is get the result of that raycast and check the distance from the object it returned. If it’s smaller than a margin it means the player is touching it.
Also you need to change the ray direction to start from the player’s position and shoot straight down:
var start = this.entity.getPosition();
var end = start.clone();
end.y -= 10; // put here any number larger than the max jump height
var result = this.app.systems.rigidbody.raycastFirst(start, end);
// now check the distance from the result, if there is one (homework!)
thanks it worked. but why when I press SPACEBAR many times my character can jump many times like flying
Make sure that you don’t allow a second jump before the previous hasn’t finished.
thank you it worked
@Unavailable
Is there any way you could paste the script in for this? Im trying to follow what you did but its not working.
Hi @ALEXANDER_CHRISTENSE and welcome!
Maybe the script from my example project can help you as well.
https://playcanvas.com/project/1063800/overview/basic-jumping