- Home /
I can't rotate a point around a pivot
public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
That is the method, and this is the calling code:
camera.transform.position = RotatePointAroundPivot(camera.transform.position,this.transform.position,new Vector3(0,5,0));
The script is attached to a cube, and I want to rotate the camera around it. The problem is, when I left click, I can see view shift slightly to the left for about half a second and then it comes back. What am I doing wrong?
In which method are you calling the code? The method itself seems to be right, but it's apparently not called the way you want it.
In the update method I just check to see if the left mouse button is down, if it is, I call it with the above code.
solved it, there was another piece of code translating the camera, and that was messing the entire thing.
Answer by christoph_r · Jun 11, 2014 at 11:41 PM
Two things: 1) I overlooked that you also need to rotate the camera (using transform.LookAt is probably the easiest solution here) because all that script does is rotate the camera position around the object. 2) It seems to do that just fine, though, because a rotation of 5° should only result in a very small tilt. Try increasing the angle to get more visible results.
It doesn't explain the camera moving back, but unless you're having other functions moving the camera, that really shouldn't happen.
Answer by noatom · Jun 12, 2014 at 01:55 AM
The above code works. My problem was another piece of code that was translating the camera.