IEnumerator ChangeSpeed(float v_start, float v_end, float duration)
{
float elapsed = 0.0f;
while (elapsed < duration)
{
speed = Mathf.Lerp(v_start, v_end, elapsed / duration);
animator.SetFloat("Walk", speed);
elapsed += Time.deltaTime;
yield return null;
}
speed = v_end;
}
Its working first time when changing to up :
private void Update()
{
var distance = Vector3.Distance(crate.position, player.position);
if (distance < 1.7f && crateOpenOnce == false && unlockCrate.HasOpened())
{
rb.isKinematic = false;
crateOpenOnce = true;
}
if(startSpeedUp)
{
if (playonce == false)
{
animator.Play("Walking");
playonce = true;
}
StartCoroutine(ChangeSpeed(0, 1, 3f));
var lookPos = target.position - player.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(-lookPos);
player.rotation = Quaternion.Slerp(player.rotation, rotation,
Time.deltaTime * rotationSpeed);
float playertargetdistane = Vector3.Distance(player.position, target.position);
if(playertargetdistane < 1f && startSlow && animator.GetFloat("Walk") == 1f)
{
StartCoroutine(ChangeSpeed(1, 0, 3f));
startSlow = false;
startSpeedUp = false;
}
}
}
First time I’m starting the coroutine like this :
StartCoroutine(ChangeSpeed(0, 1, 3f));
Its working fine than I’m starting the coroutine again this time from 1 to 0 :
StartCoroutine(ChangeSpeed(1, 0, 3f));
The first problem is how do I know if the coroutine ended first time before starting it again ? In this case I’m checking if the float value is 1 already but I’m not sure if this is the correct way to find if the coroutine ended and starting it again ?
The second problem is that when I start the coroutine second time from 1 to 0 it’s not changing it to 0 it keep staying on value 1.