Enemy stops moving when trying LERP

so everything works fine when i am using “transform.position = waypoint.transform.position;”. the enemy then moves from one tile to the other following the path list i have set, but then i try the lerp and it wont move, i added debug to parts of the code and can see they print once each to the console, here is my code.

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

public class EnemyMover : MonoBehaviour

{
//creates list of type waypoint that is our other script, then set path as a new list
[SerializeField] List path = new List();
[SerializeField] float waitTime = 1f;

void Start()
{
    Debug.Log("Start");
    //This is how a Corutine Method is called
    StartCoroutine(FollowPath());
    //repeat method and add delay and repeat time
    //InvokeRepeating("PrintWaypointName", 0, 1f); - problem with this is that it delays the whole method, we need to dealy steps in the method
    Debug.Log("Finish");
}

//set type IEnumerator returns somthing countable the system can use
IEnumerator FollowPath()
{
    // go through every object that is in the serialized field of path
    foreach(Waypoint waypoint in path) 
    {
         Debug.Log("followapath");
        //using vector3.Lerp we first create start and end position
        Vector3 startPosition = transform.position;
        Vector3 endPosition = waypoint.transform.position;
        float travelPercent = 0f;
        
        //this is to update our travelpercent while percent is under 1 because 1 is the end
        while(travelPercent < 1f)
        {
            travelPercent += Time.deltaTime;
            transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
            yield return new WaitForEndOfFrame();
            Debug.Log(waypoint.name);
        }

        //do this with every path object
        //Debug.Log(waypoint.name);
        //this creates a var that is objects transform object and the position part, then sets that as the current waypoint transform object position value 
        // this is disabled when we use LERP // transform.position = waypoint.transform.position;
        //needs yeald statment when it is a IEnumerator the return a new WaitForSecond(1f) that is value of 1 in wait in second that becomes one second.
        // so whe give up control and then we start again in 1 second.
        // this is disabled when we use LERP // yield return new WaitForSeconds(waitTime);
    }
}

}

Hi,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Does the code block with the Lerp method get called? If so, log the three variables that you use in the parentheses of the Lerp method into your console. Maybe the values do not make any sense.

if i debug to the while method like this

i get this from the console during runtime
image

if i add the debug last in the while method i only get start and finish in the console

well it seems i made a very basic mistake, i was looking in the scene view during runtime so thats why nothing was moving, so its working now thank you

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms