Question by
Kreuzritter55 · May 01, 2021 at 11:52 AM ·
coroutinelerp
Crouch Lerp Coroutine Problems
Hi, In my character controller I wanted to lerp the camera transform and Used a Coroutine to do so. The first crouch works Like it should but as soon as I try to stand up, at first it lerps to the normal position but directly after that it Lerps back to the Crouch Transform. I already tried using 2 separate Coroutines for crouch and Standup but with the same Result.
public IEnumerator LerpCrouch(Vector3 NormalPos, Vector3 CrouchPos, float duration)
{
if (isChangingPosition)
yield break;
isChangingPosition = true;
float time = 0;
while (time < duration)
{
if (isCrouching)
{
transform.position = Vector3.Lerp(CrouchPos, NormalPos, time / duration);
time += Time.deltaTime;
}else
if (!isCrouching)
{
transform.position = Vector3.Lerp(NormalPos, CrouchPos, time / duration);
time += Time.deltaTime;
}
yield return null;
}
if (isCrouching)
{
isCrouching = false;
transform.position = NormalPos;
}
if (!isCrouching)
{
isCrouching = true;
transform.position = CrouchPos;
}
isChangingPosition = false;
yield return null;
}
Comment