I need to rotate the camera around an object. It needs to be a fixed length from the Object, and must go right or left on user input. Right now I am using Cosines and Sines for it, double checked the math and it turned out right. When I try it out, the camera seems to be moving correctly until a certain point it starts making it own route. Someone have an idea/pointer? Thanks in advance!
public class CameraView : MonoBehaviour {
public GameObject center; // Object to rotate around
public float speed = 1f;
private float xcoord, ycoord, zcoord;
private Vector3 aim;
void Start () {
xcoord = transform.position.x;
zcoord = transform.position.z;
ycoord = transform.position.y;
aim = center.transform.position;
}
void Update () {
if(Input.GetKey ("right")){
xcoord = 20 * Mathf.Sin (Mathf.Asin (xcoord / 20) + speed); // Math to get new x and z position
zcoord = 20 * Mathf.Cos (Mathf.Acos (zcoord / 20) + speed);
transform.position = new Vector3(xcoord, ycoord, zcoord);
transform.LookAt(aim);
}
if (Input.GetKey ("left")){
xcoord = 20 * Mathf.Sin (Mathf.Asin (xcoord / 20) - speed);
zcoord = 20 * Mathf.Cos(Mathf.Acos (zcoord / 20) - speed);
transform.position = new Vector3(xcoord, ycoord, zcoord);
transform.LookAt(aim);
}
}
}