I’m trying to shoot a ray from the player’s camera.
The ray direction should always be in front of where the player is looking, and should always be the same as the player’s X axis, like any other shooter game.
Getting the camera’s position
Creating the direction and the ray (don’t worry about the origin here).
This is from another player’s perspective. The other player is looking directly above, yet the ray is being shot lower down.
I guess entity.camRayY should be entity.camRay.y or just camRay.y?
The direction that you calculate irritates me, do you want to put y-value of your camera ray direction with z=1 (y and z relation/x-rotation might get wrong here) to local space of player mesh, then subtract that direction by player mesh position? It seems to me that putting into local space combined with calculating a direction gives wrong result in your game world.
I would rather just modify camera ray properties (origin,direction) and/or use camera.getFrontPosition(length). To get mesh position in world space you can use mesh.absolutePosition.
// Approach #1: use camera origin
const cameraRay = camera.getForwardRay()
cameraRay.direction.x = 0
// Approach #2: set new origin like mesh or camera front
const oldDestination = cameraRay.origin.add(cameraRay.direction.normalize().scale(cameraRay.length))
const newOrigin = entity.mesh.absolutePosition or camera.getFrontPosition(1)
cameraRay.direction = oldDestination.subtract(newOrigin).normalize()
cameraRay.direction.x = 0
cameraRay.origin = newOrigin
In the end:
return cameraRay
Beware it might need some more polishing. Hopefully this helps you.
It seems to me that putting into local space combined with calculating a direction gives wrong result in your game world.
Ahh!!! I think this might be it! It would explain why it roughly works when the player looks up/down, but is slightly off.
I guess entity.camRayY should be entity.camRay.y or just camRay.y?
Since it’s a multiplayer game I need to pass in each attribute of the direction property from the client to the server. So direction.y becomes assigned to the player entity and I named it camRayY, same for x and z.
I would rather just modify camera ray properties (origin,direction) and/or use camera.getFrontPosition(length). To get mesh position in world space you can use mesh.absolutePosition.
I will try this method as soon as I’m at my computer again. Once again, thank you for your time