# Move toward an object

Hi, I am trying some basic moves to turn and move a rigidbody and not having a lot of luck. So, below, I am trying to face a gameobject (targetpoint) and move toward it. The code as shown does nothing but if I remove the rotation bit, the object (a cube) moves in a straight line down the z axis. This is, I feel, a fairly basic thing and I just cannot find an answer.

var targetRotation = Quaternion.LookRotation(targetPoint - transform.position,
Vector3(0,1,0));

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

rigidbody.MovePosition(rigidbody.position + Vector3(0,0,speed) * Time.deltaTime);

You want the rigidbody to move towards a point and look in that direction? instead of:

``````rigidbody.MovePosition(rigidbody.position + Vector3(0,0,speed) * Time.deltaTime);
``````

Do for example: ( If you want other rigidbodies to interact with the kinematic rigidbody you need to move it in the FixedUpdate function.)

``````function FixedUpdate () {
// this creates a vector from rigidbody to target
var direction : Vector3 = targetPoint - transform.position;

//normalize this vector to get the direction towards the target
direction = direction.normalize;

// Move the rigidbody towards the target in x,z.
rigidbody.MovePosition(rigidbody.position + Vector3(direction.x,0,direction.z) * speed * Time.deltaTime);

// or if you want to move in y axis as well:
// rigidbody.MovePosition(rigidbody.position + direction * speed * Time.deltaTime);
}
``````

Instead of direction you can also use transform.forward.
This code is not tested.

your Vector3(0,0,speed) is defining a world-space movement along the Z-axis , you want the local rotation , so transform.forward * speed

i.e.

``````//rigidbody.MovePosition(rigidbody.position + Vector3(0,0,speed) * Time.deltaTime);
rigidbody.MovePosition(rigidbody.position + transform.forward * speed * Time.deltaTime);
``````