Ray direction from camera rotation looking up/down

Hello,

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
image


Creating the direction and the ray (don’t worry about the origin here).
image

This is from another player’s perspective. The other player is looking directly above, yet the ray is being shot lower down.

How can I get the ray to shoot where the player is looking up or down?

Thanks :smiley:

A solution would be to use getForwardRay()-function of Camera:

Example Code:

    function predicate(mesh) {
        return mesh != ground;
    }

    let rayHelper;
    const rayLength = 100;
    scene.registerBeforeRender(function () {
        if(rayHelper)   rayHelper.dispose()
		const ray = camera2.getForwardRay(rayLength)
        rayHelper = new BABYLON.RayHelper(ray);		
		rayHelper.show(scene);		

        var pickingInfo = scene.pickWithRay(ray, predicate);

        if (pickingInfo.pickedMesh){
           console.log(pickingInfo.pickedMesh.name)
	    }
    });

You can also just use part of y-z to determine x-Axis rotation.

6 Likes

Hi there, thank you very much for your help, I really appreciate it.
I’ve changed it to this:
image

It still has the same effect… bit confused on this one :sweat_smile:

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.

1 Like

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