Hi guys,
I have script that moves an object from a to b over a percentage value… The problem is, my object is reaching b way too early; give or take around 40%. This is causing my object to pause before the next action takes place which is not what I want. Any ideas on why this is happening?
Here’s my Lerp (inside coroutine):
IEnumerator BullySquadMovement()
{
moveSpeed = .5f;
float stopDis = 3f;
float lerpTime = 1;
float currentLerpTime = 0;
float perc;
bool reachedTarget = false;
Vector2 enemyDirFromPlayer = (transform.position - player.position).normalized;
Vector2 stopPosition = enemyDirFromPlayer * stopDis;
while (true)
{
Debug.DrawLine(spawnPos, stopPosition, Color.blue);
//Debug.DrawLine(transform.position, player.position);
if (currentLerpTime < lerpTime)
{
currentLerpTime += Time.deltaTime;
}
else
{
reachedTarget = true;
}
perc = currentLerpTime / lerpTime;
Debug.Log(perc);
if (!reachedTarget)
{
transform.position = Vector3.Lerp(transform.position, stopPosition, perc); // Appears to reach target at .4 seconds
}
else
{
transform.position = Vector2.MoveTowards(transform.position, player.position, moveSpeed * Time.deltaTime);
}
yield return null;
}
}