- Home /
Rotate camera diagonally over object
How can I make the camera move around an object on the y-axis while rotating diagonally over the object? Inputs: - mouse z-axis for the y-axis rotation around the object - mouse x-axis for the diagonal "over-the-shoulder" rotation, thus modifying both y and x of camera rotation:
Here is my current script. It works, however, I have clearly made a logic-based error because when the camera is rotating diagonally over the shoulder, the cam rotates around the object on the y-axis by it's own. I just know in my case it's because I'm always updating the y axis rotation in the Quaternion "toRotation", so the y axis rotation is added to the current y axis. Maybe someone with a functioning brain comes up with a solution? Would be awesome as hell, thanks in advance!
void Update() {
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
rotationXAxis += mouseY;
rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);
float rotationYAxis = rotationXAxis;
rotationYAxis = ClampAngle(rotationYAxis, 0f, 30f);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y - rotationYAxis * Time.deltaTime, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
Vector3 position = rotation * negDistance + player.position;
transform.rotation = rotation;
transform.position = position;
player.Rotate(Vector3.up * mouseX);
mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
}
float ClampAngle(float angle, float min, float max) {
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
Your answer
Follow this Question
Related Questions
Trying to recreate the doodlejump camera c# 1 Answer
Mouse orbit and problems with raycast 0 Answers
Multiple Cars not working 1 Answer
FPS cam for Roll -a-Ball like game 1 Answer