- Home /
Rotate the camera around a gameObject with a dynamic up value.
The GameObject is moved around a sphere and I'm trying to rotate it in such a way that its up position remains the same during the rotation. The rotation happens when the right-mouse button is clicked and dragged. The problem is that when I click and drag the GameObject.up is changed to <0,1,0> which is what it would be if it were on the very top of the sphere (this would be the correct way for it to rotate if it were moving around a flat surface and not around a sphere).
this is how it looks before attempting to rotate with the right mouse button
Now when rotating the following happens depending on the position of the GameObject on the map As can be seen in the image above the GameObject.up vector value is changed.
The code used for this is as follows:
void Update ()
{
if (playerScript.paused == false) {
if (Input.GetMouseButton (1)) { //right
rotAverageX = 0f;
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotArrayX.Add (rotationX);
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt (0);
}
for (int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX [i];
}
rotAverageX /= rotArrayX.Count;
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
GameObject.Find("Player").transform.rotation = originalRotation * xQuaternion;// * yQuaternion;
//transform.localRotation = originalRotation * yQuaternion;
}
//Zoom
transform.Translate (Vector3.forward * Input.GetAxis ("Mouse ScrollWheel"));
//Move
if (Input.GetMouseButton (2)) { //middle
if (Input.GetAxis ("Mouse X") != 0) {
transform.Translate (Vector3.right * Input.GetAxis ("Mouse X"));
}
if (Input.GetAxis ("Mouse Y") != 0) {
transform.Translate (Vector3.up * Input.GetAxis ("Mouse Y"));
}
}
}
}