Help with rotating camera around object based on mouse input
I am trying to rotate the camera around the player. RotateDirection is a vector2 and comes from PlayerControls.MouseAim.ReadValue(). I think the problem is with one of the vector3/transform calculations I am trying to make. I might be using the wrong function or maybe im passing the wrong vector. I've just gone code blind staring at this block for hours and I need help.
//Last frame's camera position and rotation
//Needed to prevent camera from leaving boundaries
Vector3 lastCamLoc = camera.transform.position;
Vector3 lastCamEulers = camera.transform.eulerAngles;
//get direction of input vector from camera's perspective
Vector3 worldSpaceCameraDirection = camera.transform.TransformDirection(RotateDirection);
//Rotate 90 degrees around camera's local Z axis
Vector3 rotationAxis = Quaternion.AngleAxis(90, camera.transform.forward) * worldSpaceCameraDirection;
//Shift rotation axis to character's perspective
rotationAxis = camTarget.transform.TransformVector(rotationAxis);
//Apply rotation to camera
camera.transform.RotateAround(camTarget.transform.position, rotationAxis, RotateDirection.magnitude * Time.deltaTime * 80.0f);
//Correct camera's local z rotation
camera.transform.LookAt(camTarget.transform);
//Camera Recorrection - Has it gone past its bounds
//Use Camera's X rotation to judge camera max range
float xRot = camera.transform.eulerAngles.x;
if (xRot > maxCamRotation ||
xRot < -maxCamRotation)
{
//keep camera on boundary edge
camera.transform.position = lastCamLoc;
camera.transform.eulerAngles = lastCamEulers;
}
Comment
Your answer
Follow this Question
Related Questions
Add Rotation from parent to Quaternion Child 1 Answer
limit Quaternion.Euler rotation 2 Answers
Rotation Lerp Question 2 Answers
Maintaining Camera Rotation between modes 0 Answers
How can I set rotation properly? 0 Answers