Problems moving CharacterController from point to point.

Hi there,

I have a cutscene-like moment in my game where the first scene guides the player on rails in a first-person view through an environment. Unfortunately, I’m having quite a bit of trouble getting my Character to move from point to point.

Getting it to move from point A to point B is relatively easy, but not when it involves moving him to point C, D, F, G, etc. I’ve somehow managed to get it to move between 3 points, but after that it messes up. When it hits the 3rd point, it begins looping between point 2 and point 3. As soon as Point 3 is reached, it jumps back to Point 2 to lerp to Point 3 and repeats. I cannot figure out why it refuses to go to Point 4 and above. All my waypoints are defined and inputted. The code looks good to me. I tried changing the transform.position to adapt the endPoint once it gets there. Nothing seems to change this.

I’ll post the code. Does anyone have any idea what’s wrong here?

using UnityEngine;
using System.Collections;

public class IntroMovement : MonoBehaviour {
	
	public Transform waypoint;
	public Transform waypoint2;
	public Transform waypoint3;
	public Transform waypoint4;
	public Transform waypoint5;
	public Transform waypoint6;
	
	private bool point1 = false;
	private bool point2 = false;
	private bool point3 = false;
	private bool point4 = false;
	private bool point5 = false;
	
	Vector3 endPoint1;
	Vector3 endPoint2;
	Vector3 endPoint3;
	Vector3 endPoint4;
	Vector3 endPoint5;
	Vector3 endPoint6;

	public float duration = 1.0f;
	
	private Vector3 startPoint1;
	
	private float startTime;
	
	// Use this for initialization
	void Start () {
		startPoint1 = transform.position;
		startTime = Time.time;
		endPoint1 = waypoint.position;
		endPoint2 = waypoint2.position;
		endPoint3 = waypoint3.position;
		endPoint4 = waypoint4.position;
		endPoint5 = waypoint5.position;
		endPoint6 = waypoint6.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) {
			startTime = Time.time;
			point1 = true;
		}
		if (point1 == true) {
			transform.position = Vector3.Lerp(endPoint1, endPoint2, (Time.time - startTime)/ duration);
		}
		if (transform.position == endPoint2) {
			startTime = Time.time;
			point2 = true;
		}
		if (point2 == true) {
			transform.position = Vector3.Lerp(endPoint2, endPoint3, (Time.time - startTime)/ duration);
		}
		if (transform.position == endPoint3) {
			startTime = Time.time;
			point3 = true;
		}
		if (point3 == true) {
			transform.position = Vector3.Lerp(endPoint3, endPoint4, (Time.time - startTime)/ duration);
		}
		if (transform.position == endPoint4) {
			startTime = Time.time;
			point4 = true;
		}
		if (point4 == true) {
			transform.position = Vector3.Lerp(endPoint4, endPoint5, (Time.time - startTime)/ duration);
		}
		if (transform.position == endPoint5) {
			startTime = Time.time;
			point1 = true;
		}
		if (point5 == true) {
			transform.position = Vector3.Lerp(endPoint5, endPoint6, (Time.time - startTime)/ duration);
		}
		
	}
}

Hi @SuperSparkplug your code logic seems to be true but its too long to debug it . First try to make it simple : you can reduce your code like this :

public class IntroMovement : MonoBehaviour
{
    //use an array to store your waypoints

    public Transform[] waypoints;

    //use and index to store next target
    public int _index = 0;

    //use treshold to compare distance
    public float treshold = 0.1F;

    public float duration = 1;
    private float startTime;

    void Start()
    {
        startTime = Time.time;
    }

    private void Update()
    {
        Vector3 nextTarget;
        if (_index == waypoints.Length-1)
            nextTarget = waypoints[0].position;
        else
            nextTarget = waypoints[_index + 1].position;


        transform.position = Vector3.Lerp(waypoints[_index].position, nextTarget,
            (Time.time - startTime)/duration);

        //check whether it reach to current target
        if (Vector3.Distance(transform.position, waypoints[_index].position) < treshold)
        {
            _index++;
            startTime = Time.time;

        }

        //loop movement
        if (_index == waypoints.Length)
            _index = 0;
    }
}

use a variable to check if your character has reached to its target, Do not use equal operation to compare positions since its not precise .

hope it can help