I thought I finaly cracked the code but now it just only lerps once sigh
using UnityEngine;
using System.Collections;
public class BasicEnemyAiV2 : MonoBehaviour {
public GameObject Player;
public Vector3 FirstLerpPoint;
private Vector3 FinalLerpPoint;
public float Timer;
public float TimerStart;
public float DistanceInRay;
public float speed;
public float LerpTimer;
private bool OneTimeExecution = true;
static private Dash PlayerMovementScript;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
TimerStart = Timer;
PlayerMovementScript = Player.GetComponent<Dash>();
}
public Vector3 PlayerNewPosition;
public bool ThePlayerIsMoving;
// Update is called once per frame
void Update ()
{
ThePlayerIsMoving = PlayerMovementScript.IsMoving;
PlayerNewPosition = PlayerMovementScript.newPosition;
Debug.Log(Vector3.Distance(FirstLerpPoint, FinalLerpPoint));
if (Timer <= 0)
{
if (ThePlayerIsMoving && OneTimeExecution == true)
{
Vector3 rayDir = PlayerNewPosition - transform.position;
Ray ray = new Ray(transform.position, rayDir);
FirstLerpPoint = ray.origin + (ray.direction * DistanceInRay);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
FinalLerpPoint = PlayerNewPosition;
StartCoroutine("LerpToPos");
OneTimeExecution = false;
}
else if (OneTimeExecution == true)
{
Vector3 rayDir = Player.transform.position - transform.position;
Ray ray = new Ray(transform.position, rayDir);
FirstLerpPoint = ray.origin + (ray.direction * DistanceInRay);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
FinalLerpPoint = PlayerNewPosition;
StartCoroutine("LerpToPos");
OneTimeExecution = false;
}
}
else
{
Timer -= Time.deltaTime;
}
}
private IEnumerator LerpToPos()
{
while(Vector3.Distance(FirstLerpPoint, FinalLerpPoint) >= 4)
{
LerpTimer += Time.deltaTime;
transform.position = FirstLerpPoint;
FirstLerpPoint = Vector3.Lerp(FirstLerpPoint, FinalLerpPoint, LerpTimer * speed);
yield return null;
}
Timer = TimerStart;
LerpTimer = 0;
}
}