Hi,
I’ve created a scene where a cube follows the mouse cursor :
http://www.garethelms.org/demo/unity3d/follow/WebPlayer.html
A script rotates the cube towards the target and speeds it up the more it is facing the target. I position a yellow sphere to where the cursor is for clarity.
The problem is that the cube only ever rotates clockwise. If you move the cursor so the cube needs a negative angle to face it, it spins a +360 to face it rather than -1. For example if you circle the cursor slowly around the cube in a clockwise direction the cube will keep up and rotate towards it. If you do the same in an anti-clockwise direction the cube spins the wrong way to try and face it.
How can I rotate the cube clockwise and ant-clockwise?
Here is my follow script :
public class FollowMouse : MonoBehaviour
{
public GameObject Cursor; // This is the yellow sphere scripted to follow the mouse cursor
public float RotationSpeed = 3;
public float Speed = 10;
void Update()
{
var direction = Cursor.transform.position - transform.position;
var angle = Vector3.Angle( direction, transform.forward);
transform.Rotate( Vector3.up, angle * (Time.deltaTime * RotationSpeed));
// Divide by angle so that it slows down when facing away from target and speeds up when facing target
transform.Translate( Vector3.forward * (Time.deltaTime * (Speed / (angle / 5f))));
}
}
Sorry if this is a basic question I’m very new to vectors/quaternions/angles but I’m determined to understand it all.
Thanks
Gareth