- Home /
Object follow mouse whilst rotating around object.
I'm trying to get an object to rotate or orbit another one like a moon around the earth, it's orbit position around centre object needs to be in relation to where the mouse is on the screen. Say for example the mouse is at the top of the screen then the orbiting object would be directly above the centre. Essentially a rotatearound with the rotating objects position determined by the mouse location. Below is what I want but I'm using the GetKey function just to demonstrate it.
void Update () {
if (Input.GetKey("w")){
Vector3 centre = GameObject.Find("centre").transform.position;
transform.RotateAround (centre, Vector3.forward, 20 * Time.deltaTime);
}
}
Answer by Maerig · May 27, 2014 at 01:16 AM
You could create an empty GameObject
in your scene with a script to make it follow the mouse, for instance
public class FollowMouse : MonoBehaviour {
private float m_depth;
void Start() {
m_depth = transform.position.z - Camera.main.transform.position.z;
}
void Update() {
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y,
m_depth));
}
}
Then, you would just need to have your orbiting GameObject
as a child of the empty one, with the script which uses RotateAround
, of course.
Your answer
Follow this Question
Related Questions
Rotate One Object Around Another 1 Answer
A node in a childnode? 1 Answer
Control basics - NGUI 1 Answer
Rotate around obsolete - missing Rotate overloads??? 2 Answers
How to rotate the planar region to alway face the camera? 2 Answers