How to make object move to waypoints?

Hi,

So I’m doing some game testing and I’m tackling making my character move to several different points. To do this, I made 2 cube objects and turned them into my waypoints (defining them in the Inspector), telling my character to slerp to one point and then to another, however I’m running into a ton of glitches.

If I can get it to slerp to one place, it jumps to the second position as soon as it reaches the first end point. At worst, it just jumps to the second position automatically from the beginning. I don’t understand why it doesn’t slerp between the two points.

Here’s my code. What exactly is wrong here?

using UnityEngine;
using System.Collections;

public class CAVEMoveTest : MonoBehaviour {
	
	public Transform waypoint;
	public Transform waypoint2;
	
	private bool point1 = false;
	private bool point2 = false;
	
	public Vector3 endPoint1;
	public Vector3 endPoint2;
	public float duration = 1.0f;
	
	private Vector3 startPoint1;
	private Vector3 startPoint2;
	
	private float startTime;
	
	// Use this for initialization
	void Start () {
		startPoint1 = transform.position;
		startPoint2 = transform.position;
		startTime = Time.time;
		endPoint1 = waypoint.position;
		endPoint2 = waypoint2.position;
	}
	
	// Update is called once per frame
	void Update () {
		if (point1 == false) {
			transform.position = Vector3.Lerp(startPoint1, endPoint1, (Time.time - startTime)/ duration);
		}
		if (transform.position == endPoint1) {
			point1 = true;
		}
			if (point1 == true) {
			transform.position = Vector3.Lerp(endPoint1, endPoint2, (Time.time - startTime)/ duration);
		}
		
	}
}

There are couple of small issues that may or may not impact your fuctionaliity, but the big issue is that you are not resetting startTime when you reach endPoint1. That mean your second Lerp will have a last paramter that evaluates to something greater than 1.0 and therefore you will immediately jump to endPoint2. Insert between lines 35 and 36:

startTime = Time.time;

Note a better way to walk waypoints is to an array of Vector3s. There are a number of posts with example code using arrays on Unity Answers.