rotate object relative to other objects movement direction

Hi all,

i’m having a bit of a problem with a game i’m making for ios.
What i’m trying to do is rotate an object in the direction
of the movement of another object.

I’m having a ball (rigidbody) move around the level with the accelerometer.
Works like a charm so far.

This ball character has an object following it along the x and z axis (it’s eyes) that needs to “look at” the direction the ball is moving, so it needs to rotate around it’s y axis. So if the ball moves right the eyes need to look in that direction, and so on for every direction.

I know a bit of Javascript (i’m more of a game designer than a programmer) and have searched the answers but nothing seems to work. If anyone could help me out or point me in the right direction that’d be great !

Until then, greetings from Belgium o7

You need to detect the direction of movement. Maybe you already have this vector, or you get this vector from your accelerometer. If not, you can calculate it by comparing a previous location with the current one. Once you have that vector you do:

transform.rotation = Quaternion.LookRotation(movementVector);

Put this in the script on the object which follows the ball:

private GameObject ball;

void Start() {
	ball = GameObject.Find("ball game object's name");
}

void Update() {
	transform.LookAt(ball.transform.position);	
}