Hi, i’m currently having a problem with this specific piece of code
protected IEnumerator JumpTowards(Vector3 p)
{
animator.SetBool("jumping", true);
Vector3 currentPos = transform.position;
Vector3 targetPos = new Vector3(transform.position.x, transform.position.y + jh, transform.position.z);
while(nearbyPlayer)
{
if(transform.position.y >= targetPos.y)
{
transform.position = Vector3.MoveTowards(transform.position, currentPos, movSpeed * Time.deltaTime); Debug.Log("1");
}
else if (transform.position.y < targetPos.y)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, movSpeed * Time.deltaTime); Debug.Log("12");
}
yield return null;
}
animator.SetBool("jumping", false);
jumping = false;
yield return null;
}
This coroutine is supposed to move up (y) and object until it reaches it’s target destination and then go down to it’s original position. The problem i have is that it works perfectly when going up ( else if (transform.position.y < targetPos.y)
) but when it gets there it’s supposed to stop being called and then go down but apparently the other if statement ( if(transform.position.y >= targetPos.y)
) gets called simultaneously with the second if statement ( I know that because of the Debug.Log()).
Any help is greatly appreciated.
PD: I also checked that the whole method gets called only once.