- Home /
Arbitrary rotation around a point works except when up flips?
I've been working on the ability to rotate my camera around an arbitrary point based on mouse movement and have it working great except when it crosses the top/bottom of an axis. The code for moving the camera is:
                     // Get mouse inputs
                     float horizontalSpeed = 1.0f;
                     float verticalSpeed = 1.0f;
                     float h = horizontalSpeed * Input.GetAxis("Mouse X");
                     float v = verticalSpeed * Input.GetAxis("Mouse Y");
                     Vector3 upDirection = Camera.main.transform.up * h;
                     Vector3 rightDirection = Camera.main.transform.right * v;
                     Vector3 totalDirection = upDirection + rightDirection;
                 
                     // Set camera variables
                     Camera.main.transform.position = MathHelp.Vector3Help.RotateAroundAPoint(Camera.main.transform.position, 
                         Vector3.zero, 
                     Quaternion.Euler(totalDirection));
                     Camera.main.transform.LookAt(Vector3.zero);
Note I have to multiply the up direction by the horizontal axis, and vice versa. And the code for rotating around a point is:
         public static Vector3 RotateAroundAPoint(Vector3 point, Vector3 pivot, Quaternion angle)
         {
             // Center the point around the origin
             Vector3 finalPos = point - pivot;
             
             // Rotate the point
             finalPos = angle * finalPos;
             
             // Move back in place
             finalPos += pivot;
             return finalPos;
         }
Any thoughts?
My goal is to try and be able to rotate the camera in a sphere anywhere.
Answer by kfferic · Nov 06, 2013 at 12:57 AM
Here, delta is a Vector2 containing the mouse movement (like h and v above); center is the point you're rotating around; RotationSpeed is a speed multiplier. You might want to switch magnitude for sqrMagnitude.
 void SphericalPan(Vector2 delta, Vector3 center, float RotationSpeed)
 {
     float Angle = delta.sqrMagnitude;
     if(Angle > 0.0f)
     {
         Vector3 Axis = Vector3.Cross(delta, Vector3.forward);
         Axis=Camera.main.transform.TransformDirection(-Axis);
         Camera.main.transform.RotateAround(center, Axis, Angle * RotationSpeed);
     }        
 }
Your answer
 
 
             Follow this Question
Related Questions
Rotate vector around vector? 2 Answers
Is There An OnMouseStop? 0 Answers
Rotate so VectorY points to another Vector3 2 Answers
Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer
Rotate Cube towards Mouse position 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                