I have a y velocity value, that decreases so long as the value is greater than 0. The player is always moved up by the current y velocity. The jump function sets the y velocity to a larger number.
It’s basically a worse written version of that.
EDIT: I changed it to follow that instead, looks cleaner and works better now, but I can still only jump from the ground
EDIT2: This way makes me see what’s wrong with the system, but I don’t know how to fix it. The raycast only sees the ground. I don’t know why.
As long as you don’t check for ‘ground’ tags it should work the same on obstacle entities, because this is basically the same as a ground entity. I will check your project when I’m home.
I figured out the problem, but I still can’t fix it. I’m using raycastFirst, but I think it returns all intersections. It wouldn’t work on the obstacles because it detected the obstacle and the ground. How can I make it only return the first intersection?
EDIT: I don’t know if it’s returning all of the intersections, but something in my code doesn’t work when it returns more than 1.
It does that, and I don’t know why this works, but when I’m touching the ground it returns null. That was the only way I could find to say that you were touching the ground. Do you know a better way? I tried what was done in the post you linked, but it didn’t work.
The problem is that the start position of the raycast is too low. The origin of the player is on the ground, so the start position of the raycast is on the ground too. The raycast should start a little bit higher, above the ground. With the code below I solve this problem.
const playerPos = this.entity.getPosition();
// clone the world DOWN vector and scale it by 100 units
// then add it to the player position to get a new position 100 units down in world space
const downDir = pc.Vec3.DOWN.clone();
const startPos = new pc.Vec3(playerPos.x, playerPos.y + 1, playerPos.z);
const endPos = new pc.Vec3().copy(playerPos).add(downDir.scale(1));
const result = this.app.systems.rigidbody.raycastFirst(startPos, endPos);
if (result) {
if (this.yVel < 0) {
this.yVel = 0;
}
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
this.yVel = this.jumpHeight;
}
}
else {
this.yVel -= 9.8;
}