- Home /
My object now lerps but it only lerps once
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;
}
}
Have you done any debugging? Does the execution ever enter the while loop in the coroutine? What is th e value of`LerpTimer * speed` in your Lerp call?
As a side note, even though StartCoroutine("LerpToPos");
should work perfectly fine, I'd recommend using the IEnumerator overload of it as default (when there's nothing forcing you to use the string version). This way the compiler can help you in case you made a spelling mistake etc.
Answer by dragonking300 · Jul 24, 2017 at 07:21 AM
Unity crashed and now everything works(Just minor changes) wow.
Answer by Kishotta · Jul 24, 2017 at 05:29 AM
The problems are in your coroutine:
private iEnumerator LerpToPos() {
float lerpTimer = 0f;
// This distance will ALWAYS be greater than or equal to zero, never negative
while (Vector3.distance (transform.position, FinalLerpPoint) > 0) {
lerpTimer += Time.deltaTime;
// Lerp the transform's position instead of your endpoints
transform.position = Vector3.Lerp (FirstLerpPoint, FinalLerpPoint, lerpTimer * speed);
// Wait for the next frame before you proceed
yield return new WaitForEndOfFrame ();
}
}
Copied your coroutine changes exactly(fixed your spelling errors) but it still functions exactly the same way
Have you checked that your FirstLerpPoint and FinalLerpPoint are where you expect them to be?
actually for some reason my unity crashed I reloaded it and now it works.... buuut it only lerps towards the player position once :I
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
The enemy movement script I'm modeling after my player's isn't working. 1 Answer
Scrollrect snap laggy on mobile 0 Answers
Lerp not moving 2D object correctly 1 Answer