i have a car model that i use to make it follow the way points. Once it reaches the last waypoint it still moves and drifts off.
using UnityEngine;
using System.Collections;
public class waypoint : MonoBehaviour {
public Transform[] waaypoint;
public int speed=50;
private int currentwaypoint;
public Vector3 velocity;
// Use this for initialization
void Start () {
//Debug.Log(currentwaypoint);
}
// Update is called once per frame
void Update () {
//Debug.Log(currentwaypoint);
if(currentwaypoint< waaypoint.Length)
{
Vector3 target= waaypoint[currentwaypoint].position;
Vector3 movedirection=target-transform.position;
velocity=rigidbody.velocity;
if(movedirection.magnitude<1)
{
currentwaypoint++;
}
else
{
velocity=movedirection.normalized*speed;
}
}
rigidbody.velocity= velocity;
transform.LookAt(waaypoint[currentwaypoint].position);
if(currentwaypoint == waaypoint.Length)
{
// ??
}
}
}
this is my code…i tried a few things but didn’t work out…i’m new to unity and scripting so plz help .