- Home /
Help with 3rd person camera
In short I want to recreate the 3rd person camera from the game Unturned. In it, the camera is a bit behind and hovering over the player (over the shoulder camera), while the character is always looking forward at the cursor/center of the screen (the cursor is CursorLockMode.Locked). Basically a 3rd person aim camera.
I have this code:
float x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float y = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
// transform = camera object
// transform.parent = EmptyObject (with character controller)
transform.RotateAround(transform.parent.position, Vector3.left, y);
transform.parent.Rotate(Vector3.up * x);
which comes to the thing I want, really close, but there is a big issue that I tried to figure out so I can explain it, but I couldn't. You could also do something like:
transform.parent.Rotate(Vector3.left * y);
transform.parent.Rotate(Vector3.up * x);
Again close but not really, it kind-a starts rotating on the z axis.
Answer by HarryBGamer2570 · Jan 17 at 08:54 PM
I actually found a wait to do it.
First I split the players character (a cylinder) into top (body) and bottom (legs) part. Then I made an anchor point for the top part that is at the cut between both parts, so the top part doesn't rotate around it's self, but around the anchor point.
Then with C#
playerObject.transform.Rotate(Vector3.up * x);
-rotate the player, which actually is just a character controller
if (rotation < 90 && rotation > -90)
{
playerObject.transform.Find("Anchor").transform.Rotate(Vector3.left * y);
//playerObject contains the Legs and the Anchor, to which (the anchor) the body is parented to
}
-rotate the anchor (and with it the body) in the direction the cursor is (vertical), but limiting the rotation of the body so it doesn't go rotating 360
then with:
playerObject.transform.Find("Legs").transform.rotation = playerObject.transform.rotation;
playerObject.transform.Find("Anchor").transform.eulerAngles = new Vector3(playerObject.transform.Find("Anchor").transform.eulerAngles.x, playerObject.transform.Find("Legs").transform.eulerAngles.y, 0f);
-rotate the legs where the cursor is pointing at (horizontal axis) -and then with the legs rotate the body with them, but with global/world axis
But we wouldn't want only in third person out character to be able to bend over or look at the sun, so for first person basically everything till now, without the last line (where we rotate globally).
This should be it, at least for me it is, because it does everything I need it to do. Here are some results:
Your answer
![](https://koobas.hobune.stream/wayback/20220613051323im_/https://answers.unity.com/themes/thub/images/avi.jpg)