- Home /
How to rotate the camera to the gameobject position.
I have a simple sphere Gameobject at position (0, 0, 0). My camera has the beginning rotation (0, 0, 0). Now i have set the inputs MouseX and MouseY to get the x, y and change the position of the camera.
if (mouseDown){
x = Input.GetAxis("MouseX");
y = Input.GetAxis("MouseY");
}
That works all fine, but now i want to set the camera rotation to the center of the Sphere (0, 0, 0). So that I can rotate around (with holding mouse0) the sphere. I tried it with
camera.transform.rotation.SetFromToRotation(camera.transform.position, target.transform.position);
but it don't work.
I'm sure that it easy but i don't find the solution. I'm absolutely new in Unity. Thanks.
Answer by Loius · Feb 14, 2013 at 08:34 PM
The simplest solution is to create a pivot object for the camera (as the parent of the camera), and move/rotate that object instead of the camera itself.
It is necessary that the camera rotates around the gameObject. I want to create a astroid as the center and rotate the camera (with mouse0 key down) around the object in the same distance.
I already tried:
camera.transform.rotation.SetFromToRotation(camera.transform.position, target.transform.position);
but that don't work.
Answer by Chryb · Feb 15, 2013 at 01:53 AM
Now, the Camera has the right rotation. I realized that with camera.transform.LookAt(astroid.position);, but now i have a other problem. The position of the camera is not perfect set and the camera don't stop to rotate around the astroid if i start to drag the mouse.
That I use for the position:
if (mouseDown){
x += Input.GetAxis("MouseX");
y += Input.GetAxis("MouseY");
}
SetCameraRotation();
SetCameraPosition(rotation, x, y);
}
void SetCameraRotation(){
camera.transform.LookAt(astroid.position);
}
void SetCameraPosition(Quaternion rotation, float x, float y){
Quaternion unit = Quaternion.Slerp(camera.transform.rotation, astroid.position, Time.deltaTime * 3);
Vector3 position = unit * new Vector3(x * 0.1f, y * 0.1f, 0.0f) + camera.transform.position;
camera.transform.position = position;
}
I hope someone can help me.
Thats the new code:
if (mouseDown){
x += Input.GetAxis("$$anonymous$$ouseX");
y += Input.GetAxis("$$anonymous$$ouseY");
}
Quaternion rotation = Quaternion.Slerp(camera.transform.rotation, Quaternion.Euler(x, y, 0), Time.deltaTime * 3);
SetCameraRotation();
SetCameraPosition(rotation, x, y);
x = 0;
y = 0;
}
void SetCameraRotation(){
camera.transform.LookAt(astroid.position);
}
void SetCameraPosition(Quaternion rotation, float x, float y){
Vector3 position = rotation * new Vector3(x * 3, y * 3, 0.0f) + camera.transform.position;
camera.transform.position = position;
}
Answer by robertbu · Feb 16, 2013 at 01:09 AM
My understanding is that you have an object at the origin (asteroid), and you want a camera "above" the asteroid looking "down" (towards the origin). And you want to move the mouse to rotate the camera round the asteroid as if you are skimming along the surface looking down.
To do this, I would 1) place an empty game object at the center of rotation, 2) place the camera the right distance looking at the asteroid, 3) make the camera a child of the empty game object, and 4) attach a script to the empty game object that rotates the empty game object. The camera will be taken along for the ride and remain a fixed distance above empty game object. Here is simple few lines of code based on your GetAxis() above to rotate the empty game object:
void Update () {
if (Input.GetMouseButton(0)){
Vector3 v3T = new Vector3(Input.GetAxis("Vertical"),-Input.GetAxis("Horizontal"),0.0f);
transform.Rotate (v3T *0.1f);
}
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Aircraft wing position 0 Answers
Scripting Issue With Top Down Mouse Movement 1 Answer
Changeing Camera Position 1 Answer
Rotating Camera around Player at Certain Point in Map 0 Answers