# How to rotate a game object in a given path?

Hello, guys. I have a game object that will follow the path given to move. But now i face a problem, how can i add some rotate coding in the pathfollower coding? What kind of rotate coding that i can use?
This is the pathfollower coding:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathFollower : MonoBehaviour {

``````public Transform[] path;
public float speed = 5.0f;
public float reachDist = 0.5f;
public int currentPoint = 0;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

//Vector3 dir = path [currentPoint].position - transform.position;

float dist = Vector3.Distance(path[currentPoint].position, transform.position);

transform.position = Vector3.MoveTowards (transform.position, path[currentPoint].position, Time.deltaTime * speed);

if ( dist <= reachDist )
{
currentPoint++;
}

if ( currentPoint >= path.Length)
{
currentPoint = 0;
}

}

void OnDrawGizmos ()
{
for ( int i = 0; i< path.Length; i++)
{
if (path *!= null)*
``````

{
Gizmos.DrawSphere(path*.position, reachDist);*
}
}
}
}
Thanks all.

Hello @KeyinPhua.

You can set the rotation of an object simply by adding this line of code:

``````transform.rotation = Quaternion.LookRotation(path[currentPoint].position - transform.position);
``````

If you would like to lock Y axis of the rotation. You can use it like this:

``````Vector3 rotationVector = path[currentPoint].position - transform.position;
rotationVector.y = 0;

transform.rotation = Quaternion.LookRotation(rotationVector);
``````

This method is simply rotating object into vector direction.

All the best,

The Knights of Unity

I’ve added the transform.lookAt to your code, seems like the easiest way to get the job done. However, I would strongly suggest using a tweener, like DOTween, based on the type of actions you are doing in your game.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathFollower : MonoBehaviour
{

public Transform[] path;
public float speed = 5.0f;
public float reachDist = 0.5f;
public int currentPoint = 0;
// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
//Vector3 dir = path [currentPoint].position - transform.position;
float dist = Vector3.Distance(path[currentPoint].position, transform.position);
transform.position = Vector3.MoveTowards(transform.position, path[currentPoint].position, Time.deltaTime * speed);
transform.LookAt(path[currentPoint].position);
if (dist <= reachDist)
{
currentPoint++;
}

if (currentPoint >= path.Length)
{
currentPoint = 0;
}

}
void OnDrawGizmos()
{
for (int i = 0; i < path.Length; i++)
{
if (path *!= null)*
``````

{
Gizmos.DrawSphere(path*.position, reachDist);*
}
}
}
}