- Home /
How to rotate an object to face the direction it's going?
Hello, I currently have a waypoint system but I can't seem to figure out how to make the object face the direction it's going. This is the code for my waypoint system.
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour
{
private Transform startMarker, endMarker;
public Transform[] waypoint;
private float speed = 1.0F;
private float startTime;
private float journeyLength;
// public Transform target;
private int currentStartPoint;
void Start ()
{
currentStartPoint = 0;
SetPoints (); //Function om de waypoints in te zetten
}
void SetPoints ()
{
startMarker = waypoint [currentStartPoint];
endMarker = waypoint [currentStartPoint + 1];
startTime = Time.time;
journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
}
void Update ()
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
if (fracJourney >= 1f && currentStartPoint + 1 < waypoint.Length) {
currentStartPoint++;
SetPoints ();
}
}
}
Comment
Answer by AndresBarrera · Jan 20, 2015 at 12:19 PM
Try this: Quaternion.LookRotation. Look at the sample code