Hi, I am trying to work out how to achieve the above in Unity.
I have a planet with a satellite which can orbit around the centre point of the planet. I want to be able to click anywhere on the screen and have the satellite smoothly animate round to line up with the mouse click. The satellite should move in whichever is the shortest direction to line up with the click.
I’m new to Unity and I’m porting this functionality over from a pure C++/Direct X project. I guess I can just port all the maths over from this project, but I wondered if there was a more straight forward way of achieving this in Unity.
Just an update on where I am with this for anyone that is interested. I found this post which show how to calculate the direction of rotation. I have implemented this in my script, which is attached to the satellite and it now largely works.
My source is as follows:
private var direction : int;
function Start () {
direction = 0;
}
function Update () {
if (Input.GetMouseButtonDown(0))
{
var mouseWorldPosition =
Camera.mainCamera.camera.ScreenToWorldPoint(new Vector3(
input.mousePosition.x, Input.mousePosition.y, 1));
mouseWorldPosition.z = 0;
direction = AngleDir(transform.forward, mouseWorldPosition, transform.up);
}
if (direction > 0)
{
transform.RotateAround (Vector3.zero, Vector3.back, 20 * Time.deltaTime);
}
else if (direction < 0)
{
transform.RotateAround (Vector3.zero, Vector3.forward, 20 * Time.deltaTime);
}
else
{
// Do nothimg
}
}
function AngleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
var perp: Vector3 = Vector3.Cross(fwd, targetDir);
var dir: float = Vector3.Dot(perp, up);
if (dir > 0.0)
{
return 1.0;
}
else if (dir < 0.0)
{
return -1.0;
}
else
{
return 0.0;
}
}
Clicking anywhere on the screen now sets the satellite off in the correct direction. I now just need to work out how to stop it when it reaches the correct position.