- Home /
How can I rotate the camera around an object?
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);
}
}
}
Answer by robertbu · Sep 18, 2014 at 01:27 PM
Assuming the object you are rotating around is stationary, you can use Transform.RotateAround():
#pragma strict
public var speed : float = 135.0; // Degrees per second
public var object : Transform;
function Update() {
transform.RotateAround(object.position, Vector3.up, -Input.GetAxis("Horizontal") * speed * Time.deltaTime);
}
I'm using the 'Horizontal' axis, which by default maps to the arrow keys and/or the a & d keys.
Works like a charm! Thanks a lot! For future debugging, can you tell me what I could have done wrong/better in my previous code?
Without spending some time, I cannot tell you exactly what is wrong with your code, but two things jump out at me. First, the equation for $$anonymous$$r to Rect (which is what you should be doing for circling around an object) is:
x = r * cos(theta);
y = r * sine(theta);
As for Asin() and Acos() in the equation, seems to me that you just want to increment and decrement an angle, and in doing so, you want your speed to be multiplied by Time.deltaTime. So the code would be something like this:
if(Input.Get$$anonymous$$ey ("right")){
angle += speed * Time.deltaTime;
xcoord = 20 * $$anonymous$$athf.Cos (angle);
zcoord = 20 * $$anonymous$$athf.Sin (angle);
transform.position = new Vector3(xcoord, ycoord, zcoord);
transform.LookAt(aim);
}
And this code will only work if your object is at the origin. If you want an offset, you'll have to add that.
transform.position = new Vector3(xcoord, ycoord, zcoord) + offset.
Note this can all be done with Vectors as well, which is the typical way you see problems like this one solved (if RotateAround() did not exist).
Your answer
Follow this Question
Related Questions
Camera to follow the player in the form of radius 1 Answer
Flip over an object (smooth transition) 3 Answers
How to smoothly rotate an object through just one axis 1 Answer
I need the sound of clothes to play when the camera rotates. 2 Answers
Getting the rotation of an object does not return correct rotation 1 Answer