Problem switching camera frm 1st person to 3rd person and back

Hello,

I got my camera as a child of the Player. It has starting Position 0, 1.6, 0.25.
I always render my Player so if I am looking down, I see the human mesh under the shoulders.
Works very well.

When I press “C” the camera Switches a boolean value and is a 3rd Person camera now.

I am changing the Position of the camera in this case (behind the Player but a bit higher) and use transform.LookAt (target); to have my Player centered.

When I Switch back to 1st person, I reset the Position. But now the camera is behind or inside the head, or sometimes right of it etc.

Can anybody tell me what am I missing here?

} else if (Input.GetKeyDown (“c”))
{
Globals.Camera3rdPerson = !Globals.Camera3rdPerson;
if (Globals.Camera3rdPerson)
{
Camera.main.fieldOfView = 100;
Camera.main.transform.rotation = Globals.PlayerTransform.rotation;
}
else
{
Camera.main.fieldOfView = 96;
Camera.main.transform.position = new Vector3(Globals.PlayerTransform.position.x+0.0f,Globals.PlayerTransform.position.y+1.6f,Globals.PlayerTransform.position.z+0.25f);
Camera.main.transform.rotation = Globals.PlayerTransform.rotation;
Camera.main.transform.position = new Vector3(Globals.PlayerTransform.position.x+0.0f,Globals.PlayerTransform.position.y+1.6f,Globals.PlayerTransform.position.z+0.25f);
}
} else

Thanks a lot and have a nice sunday,

Firlefanz

If it’s slightly off from where it’s supposed to be, you might be getting floating point precision errors. For example, if the player is currently at xyz: 0.123894, 1.2848, 10.48183 and you add 0, 1.6, 0.25 to that, you’re going to get some small errors.

A solution might be to parent the camera to the player when switching back to first person and explicitly setting your offset position.

Try having 2 cameras & switch between the 2?

1 Like

With 2 cameras it works very good, thank you guys for both the hints :slight_smile: