[SOLVED] Raycast for jump in FPS

Hello. I want to find out how to get the distance from the player to any object directly below it with raycast.
This is necessary to allow the jump. Because Collision Event has some problems. Thanks!

Here is some quick code to do a raycasts downwards from a given position:

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 endPos = new pc.Vec3().copy(playerPos).add(downDir.scale(100));

const result = this.app.systems.rigidbody.raycastFirst(playerPos, endPos);

   const distance = result.point.distance(playerPos);

Thank you, Leonidas :pray:

One note, in practice you may want to start the raycast below the player’s feet. Otherwise you may be raycasting and always getting as a result the player model.

1 Like

how to? Do i need to create one more entity?

1 Like

One easy way is to add an empty entity as child to the player, locate it just below its feet. Then in code grab a reference to that entity and gets its feet, instead of the player pos:

const feetPos = this.entity.findByName('Feet').getPosition();
1 Like

Yeah, Thanks! <3

1 Like

this counts from the lowest located ridigbody. For example, a two-storey house. if you are on the second floor, the code distance = from the ground first floor to the player.

i just changed downDir.scale(100) to downDir.scale(1). Now it is works good

1 Like