Contrary if statements being called at the same time
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.
Answer by Bunny83 · Oct 10, 2020 at 01:08 AM
No, it's not called simultaneously, you just jump between the two every frame. Your first if statement only moves down towards the original position if your the current position of your object is greater or equal to your target position. Once you moved one tiny step downwards your object is no longer above your target position and therefore your second condition will take action and you move back up. The next frame the first condition will be true again and you again move down a tiny bit. This repeats endlessly until nearbyplayer is false.
If you want it constantly move up and down while that boolean is true, do something like this:
while(nearbyPlayer)
{
while(transform.position.y >= targetPos.y)
{
transform.position = Vector3.MoveTowards(transform.position, currentPos, movSpeed * Time.deltaTime); Debug.Log("1");
yield return null;
}
while (transform.position.y < targetPos.y)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, movSpeed * Time.deltaTime); Debug.Log("12");
yield return null;
}
yield return null;
}
If you want to be able to interrupt the movement as soon as the boolean turns false, you can just include it in the condition of the two inner while loops.
Your answer
Follow this Question
Related Questions
Play animation when WASD is pressed, stop animation when none of them are pressed (Help!!) 0 Answers
How to cancel a if statement once touching a collider 1 Answer
how can i repeat an if statment for x sec? 1 Answer
Smooth Forward Movement with a CoRoutine 1 Answer
Rotating object with Lerp in coroutine with correct speed 0 Answers