How to rotate and move player mesh in camera direction

Hi guys,

How do I rotate and move a player mesh in the direction a camera (ArcRotateCamera) is looking at? I have been at this for a while still no luck. Please I need help!!!

I have prepared a playground to show what the issue is. Basically, I’m using ArcRotateCamera [ camera.alpha ] to rotate the camera but I need the mesh to rotate and face the camera direction when I move the mesh, thanks. :slight_smile:

https://playground.babylonjs.com/#D7DIJE#10

Maybe try this?: player.lookAt(camera.getForwardRay().direction)

2 Likes

Thanks for the quick reply, I will give it a try :slight_smile:

Hi there,

The code you posted really did help as I was able to find different results. I have managed to implement what I want but I’m stuck with one last issue.

Basically, I would like to move/rotate the player independently without being affected by the camera and also move/rotate the player in the forward direction when the camera is rotating.

I managed to get the movement and camera part working but the player isn’t rotating independently in the right(angle) direction. (I’m currently using a gamepad analogue for this but I have implemented similar behaviour in the playground)

I have also updated the playground to properly reflect the issue.
https://playground.babylonjs.com/#D7DIJE#39

Many thanks.

I think you can design like 3rd player controller, once you start move,you store the start-time camera forward and right and then move base this forward and right. you can move and rotate camera around at the same time

Maybe @PolygonalSun has a trick up his sleeve for it ?

I’m not 100% sure that I understand what you’re going for but it sounds like you’re just trying to make it so that your player either rotates with the camera rotation or rotates independently while the camera stays in place (kind of like “Tank/Resident Evil 1” controls). Is that correct?

@PolygonalSun Yeah, exactly that!

Ok, I have implemented another way, this time the keyboard arrow keys are used to determine both move and rotation of the player but the player’s forward direction is messed up when the camera is rotated, this should better illustrate the issue.

Controls

A / D to rotate the camera
Arrow Keys to move the player.

https://playground.babylonjs.com/#64NDCV#1

The above controls also apply to the first playground code.

Based on what I’m seeing, the reason your movement isn’t working is because you’re moving your player in an absolute direction (move on x-axis for horizontal/y for vertical), rather than moving relative to your camera. There are probably a few ways to do this but a simple way to do it is to use your ArcRotateCamera’s alpha angle and calculate the angles of movement with some trigonometry: ArcRotateCamera Third Person Controls 2 | Babylon.js Playground (babylonjs.com). While this isn’t the best approach, this just illustrates a way to account for camera angle. I do feel like there’s a better way to do this but I can’t quite remember it off the top of my head.

1 Like

@PolygonalSun Your code update is what I have been chasing after. :slight_smile: I will integrate it and get back to you, many thanks.

1 Like

Ok, I have tried but no luck yet. @PolygonalSun Would mind integrating your code into this playground? as this one uses a gamepad instead of a keyboard. I believe I’m missing something really small here but if the gamepad one works then I should be able to integrate the solution properly, thanks for the help.

https://playground.babylonjs.com/#0B8PM2#4

Controls:

Left Stick to Rotate/Move player
Right Stick to Rotate camera

Hi guys, I finally managed to fix it. I mean it works as expected and I’m happy with the result. :slight_smile: Thanks for the help again!. I have updated the playground for anyone interested. :slight_smile:

https://playground.babylonjs.com/#0B8PM2#6

A gamepad is required to test but can easily be modified for keyboard input.

Left Stick to Rotate/Move player
Right Stick to Rotate camera
1 Like