0

I'm new to game development, and I've been trying to code a "smooth camera" rotation that's limited within the camera's local rotation, using a smoothed cube and transform.lookAt. The code works fine when parent is rotated straight on, but begins to flicker when the parent is rotated -90 degrees, and becomes completely reversed when rotated -180 and +180.

Here's a video of the issue in play: https://streamable.com/sqzyb4

and here's the code:

[Header("References")]
public Camera cam;
public Transform cursorDelay;
public Transform cameraHandler;

[Header("Options")] public float smoothSpeed; public float zOffset;

[Header("Camera Restrictions")] public float maxCameraRotationX; public float maxCameraRotationY;

[Header("Sensitivity/Reach")] public float cameraXSensitivity; public float cameraYSensitivity;

private float camX; private float camY;

private void Start() { Cursor.lockState = CursorLockMode.Confined; }

private void Update() { //Takes mouse position and lerps cursorDelay with it, smoothing the camera movement.

Vector3 cursorPos = Input.mousePosition;
cursorPos.z = zOffset;
Vector3 smoothedPos = Vector3.Lerp(cursorDelay.transform.position, cam.ScreenToWorldPoint(cursorPos), smoothSpeed * Time.deltaTime);
cursorDelay.transform.position = smoothedPos;

cam.transform.LookAt(smoothedPos);

CameraRestrictions();

}

//Restricts Camera's local rotation private void CameraRestrictions() { camX = cam.transform.localRotation.x; camY = cam.transform.localRotation.y;

camX += Mathf.Clamp(camX, -maxCameraRotationX, maxCameraRotationX);
camY += Mathf.Clamp(camY, -maxCameraRotationY, maxCameraRotationY);

cam.transform.localRotation = Quaternion.Euler(camX * cameraXSensitivity, camY * cameraYSensitivity, 0f);

}

If anyone knows a fix or something I did wrong, please let me know.

Evorlor
  • 5,793
  • 9
  • 55
  • 99
NovaMaxi
  • 1
  • 1
  • Remember, localRotation.x/y/z are not angles, but imaginary components of a 4-dimensional quaternion. It is not safe or geometrically meaningful to modify them individually. You may be interested in past Q&A like Limit rotation angle or How to express and validate Euler rotation limits in Unity. You probably also don't want a += on your clamp lines = Mathf.Clamp(... is more likely what you meant. – DMGregory Oct 31 '23 at 23:02
  • Thank you so much! The posts didn't exactly solve my problem, but it definitely pointed me into the right direction for fixing it. – NovaMaxi Nov 01 '23 at 13:12
  • Does that mean you found a solution you can post as an answer below, or do you still need help executing your solution? If the latter, please edit your question to clarify what help you need. – DMGregory Nov 01 '23 at 15:34

0 Answers0