- Home /
Prevent Z rotation on 3rd person camera
I'm relatively new to unity and have been trying to make a 3rd person camera work with mouse movements. So far I have it working sort of but currently I'm getting rotation on the z axis as well despite never telling the z axis to rotate. I'm aware that this is because of the way axis rotate however I have no idea how to solve the solution. My current code is as follows:
//Mouse Look
float rotx = Input.GetAxis("Mouse X") * lookSpeed * Time.deltaTime;
float roty = Input.GetAxis("Mouse Y") * lookSpeed * Time.deltaTime;
playerTransform.RotateAround(playerPos, Vector3.up, rotx);
camTransform.RotateAround(playerPos, Vector3.right, -roty);
I know that you can solve this problem when using normal rotate by using space.world but I have no idea how to solve this with rotate around.
Answer by Voavo · Aug 14, 2018 at 12:28 AM
Try this:
float lookSpeed=3f;
float rotx=0;
float roty=0;
void Update () {
rotx += lookSpeed * Input.GetAxis ("Mouse X");
roty -= lookSpeed * Input.GetAxis ("Mouse Y");
camTransform.eulerAngles = new Vector3 (roty, playerTransform.eulerAngles.y, 0.0f);
playerTransform.eulerAngles = new Vector3 (playerTransform.eulerAngles.x, rotx, 0.0f);
}
Your answer
Follow this Question
Related Questions
Importing Levels for 3D Program to Unity? 1 Answer
Skybox look really creepy after reinstalling pc 1 Answer
Spawning 2 objects in exactly the same place!!! 1 Answer
Jumping Not Ever Time I Hit The Space Bar 1 Answer
Nav Mesh Agent 0 Answers