I’m setting up a camera that’s supposed to lock when it’s between two angles to keep it from going too high or too low and inverting. I’ve been following this tutorial:
Problem is, while it WORKS, it’s jittering. I think it’s rotating past the point that it’s not supposed to, and then it’s being reset back to the value over and over. I tried a bunch of different timings and ordering for the updates and the commands. I hope this code isn’t too hard to read. I’ve been taking what I learned from the tutorial and changing it to fit my needs, like adding the ability to not rotate while standing still.
(There are a few confusing variables like cameraRotationLocker and cameraTarget, but basically those are just some debugging things I did to help me illustrate previous problems and even fix them.) cameraTarget is an empty object inside the player that can rotate on two axis, while cameraRotationLocker is an empty object that can only rotate on the Y.
Any help would be super appreciated.
void LateUpdate()
{
if (cameraTarget.rotation.eulerAngles.x > 45f && cameraTarget.rotation.eulerAngles.x < 180f) { // Locks the camera from going too high up
cameraTarget.rotation = Quaternion.Euler(45f, 0, 0);
}
if (cameraTarget.rotation.eulerAngles.x > 180f && cameraTarget.rotation.eulerAngles.x < 315f) { // Locks the camera from going too far down
cameraTarget.rotation = Quaternion.Euler(315f, 0, 0);
}
float horizontal = Input.GetAxis("Mouse X") * cameraSpeed * Time.deltaTime;
float vertical = Input.GetAxis("Mouse Y") * cameraSpeed * Time.deltaTime;
if (Input.GetKeyDown("w")) { //Rotates the player to the rotation locker on walk, once
player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, cameraRotationLocker.transform.rotation, 360);
cameraRotationLocker.transform.rotation = player.transform.rotation;
cameraTarget.transform.rotation = cameraRotationLocker.transform.rotation;
Debug.Log("Walking");
}
if (playerMoving) { //Rotate the Player AND Camera if the player is moving
cameraTarget.Rotate (-vertical, 0, 0); // Rotates just the camera on the X
player.Rotate(0, horizontal , 0); // Rotates the player and the camera on the Y
float desiredYAngle = cameraRotationLocker.eulerAngles.y;
float desiredXAngle = cameraTarget.eulerAngles.x;
cameraRotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
}
else { // Rotates ONLY the Camera if the player isn't moving
cameraTarget.Rotate (-vertical, 0, 0); // Rotates just the camera on the X
cameraTarget.Rotate (0, horizontal, 0, Space.World); // Rotates just the camera on the Y
cameraRotationLocker.Rotate (0, horizontal, 0); // Rotates the Camera Locker on the Y
float desiredXAngle = cameraTarget.eulerAngles.x;
float desiredYAngle = cameraTarget.eulerAngles.y;
cameraRotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
}
Vector3 desiredPosition = player.position - (cameraRotation * offset);
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, cameraSmoothing);
transform.position = smoothedPosition;
transform.LookAt(player);
}