First person movement - rotating camera relative to player model

Hello

I am trying to implement a first person camera where the player model moves relevant to which direction the camera is facing. Saw lots of topics on this but I couldn’t find a solution to be able to implement this correctly for first person. I want to make it so the camera is in the players eyes and the model moves according to the camera.

Below is what I have to rotate the player model relevant to the camera direction which works but has some issues.

this.playerModel.setLocalEulerAngles(this.camera.getLocalEulerAngles());

The first issue is when you move the mouse up and down the model with rotate up and down too ( i do not know how to explain this ). Below is a video of what I mean.

So instead of the player model fully moving up/down I want to achieve just the body/arms moving up and down to simulate as if the player is looking up and down.

The second issue I got was the rotation of the model. The rotation of the model is for some reason facing towards the camera and not away from it even after I change the rotation of the model it always faces toward the camera. For reference I just got some free model and animation from mixamo.

My brain is fried trying to figure this out all day so I am asking the forum for help :grinning:

Hi @nasjarta :wave:,

this.playerModel.setLocalEulerAngles(this.camera.getLocalEulerAngles());
this.playerModel.rotate(0, 180, 0); // add this line and try
1 Like

Hello

I tried your line and but that will be constantly rotating the playermodel to that direction every game tick which I think would not be the best approach but thanks anyway.

I made a change to the title of the topic just so it might be confusing. I will be using the first person movement script along side with this for better understanding. This is a topic that has been asked lots of times in the forum but I have not been able to find a working answer. I hope to be able to link this to the first person movement script and use this as a future reference for people who come across this issue.

I managed to understand why the player model is facing towards the camera because the player model itself is being rotated towards the cameras euler angels with the above line. I think the best way to override this is using a parent object like so:

Player entity (with collision/rigidbody) → Parent entity → camera entity & player model entity

so instead of playerModel.setLocalEulerAngels need to set parentEntity.setLocalEulerAngels

That way you can rotate the parent entity and both the camera and player model will move accordingly to that entity.

The problem is I am having a hard time understanding how to make it work properly. Below is some code I tried in order to get it to work

var rotation = new pc.Vec3(0, this.eulers.x, 0);
this.parentEntity.setLocalEulerAngles(rotation);

With this the player model is facing away from the camera and it rotates without falling over. However the camera rotation becomes faster while the player model rotates slowly.

var rotY = 0;

rotY += this.eulers.x * this.lookSpeed;
this.parentEntity.setLocalEulerAngles(0, rotY, 0);

Here is another code I tried in order to fix the above issue but still the same. The Euler angles of the parent entity are maybe clashing with the cameras Euler angles which makes the look speed of the camera faster than what it should be?

If it is really confusing here is a picture of what I want to achieve in pictures. The blue is the camera.

Here is what should not bee happening:

I have been searching unity forums for some days now but the answers are hard to implement to playcanvas as they are always using quaternions.

If somebody can point me in the correct direction of the logic, how I can achieve this and I would be really happy. Maybe I am over complicating this too much.

I will link a new project link here

Are you trying to do similar to this?
Gamepad Third Person Controller

1 Like

That is very useful thanks!

It is exactly what I want once you hold left/right click and turn around in that project, the player follows where the camera is facing.

I will play around with it and see if I can incorporate it for first person.

I think I am understanding a bit how to achieve this I think I will need separate rotations for body and legs in order to simulate where the player/camera is looking.