ā while statements
Hello im stuck on a small part of my code im trying to use a while statement but its crashing the game. I dont want multi jumps to go < 0 is there a way to do that with the "multiJump--;"?
if (gI.jumpInput == true)
{
while (multiJump > 0)
{
if (grounded)
{
rb.velocity = new Vector2(gI.valueX * speed, jumpForce);
multiJump--;
}
}
}
gI.jumpInput = false;
multiJump = 1;
17 Replies
while
will loop as fast as it can, if it's a hot-spot it may freeze it indefinitely, which means it will hog the game thread, which results in the game freezing.yeah thats what its doing haha
So. Avoid while loop there.
Just use the Update loop
this is Unity correct?
yeah
What do you want to actually achieve with the while loop?
Are you sure you're not just looking for an if statement?
i just want while i jump if the multijump is set to 2 jump 2 times then land and set jump to 1
i have on collision earlier that is multijump = 2
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.layer == jumpPad)
{
multiJump = 2;
}
}
So you're looking for an if statement then
if the multijump is set to 2 jump 2 times then land and set jump to 1Update is called every frame, so there's literally no reason to use a while loop
ahh ok thanks for that
i will see what i can do
then contain the while loop inside the if statement
for the execution for jump
No, completely remove the while loop
You have no need for it.
also I would not recommend using layers to check whether it's a jump pad or not
As you can only have 31 different layers
im still learning haha thanks for your time, should i be checking for scripts?
or is there a way to check for game objects directly
I highly recommend to check for tag using
.CompareTag("JumpPad")
a way to do that with collision?
thanks i will get that sorted now
and i will use trigger instead of collision
If the jump pad also has a specific force attached to it, you can also retrieve the script attached to the transform.
such as
yeah we have "Jump force" to counter act gravitiy so i can change that also thanks for the help time to try and look after a dog and do this at the same time
š
if (gI.jumpInput == true)
{
if (grounded == true)
{
rb.velocity = new Vector2(gI.valueX * speed, jumpForce);
}
else if (additionalJumps > 0)
{
rb.velocity = new Vector2(gI.valueX * speed, jumpForce);
additionalJumps -= 1;
}
}
gI.jumpInput = false;
then it resets when my raycast hits the ground š thank you @LLVM and then i call this function in "FixedUpdate" i have done "public int additionalJump;" so now i can make the jump pad change the variable when i need to thanks again for pointing me in the right tracks
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.