C
C#•2y ago
TheFatMike

ā” 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
Buddy
Buddy•2y ago
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.
TheFatMike
TheFatMikeOP•2y ago
yeah thats what its doing haha
Buddy
Buddy•2y ago
So. Avoid while loop there. Just use the Update loop this is Unity correct?
TheFatMike
TheFatMikeOP•2y ago
yeah
Buddy
Buddy•2y ago
What do you want to actually achieve with the while loop? Are you sure you're not just looking for an if statement?
TheFatMike
TheFatMikeOP•2y ago
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; } }
Buddy
Buddy•2y ago
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 1
Update is called every frame, so there's literally no reason to use a while loop
TheFatMike
TheFatMikeOP•2y ago
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
Buddy
Buddy•2y ago
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
TheFatMike
TheFatMikeOP•2y ago
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
Buddy
Buddy•2y ago
I highly recommend to check for tag using .CompareTag("JumpPad")
TheFatMike
TheFatMikeOP•2y ago
a way to do that with collision?
TheFatMike
TheFatMikeOP•2y ago
thanks i will get that sorted now and i will use trigger instead of collision
Buddy
Buddy•2y ago
If the jump pad also has a specific force attached to it, you can also retrieve the script attached to the transform. such as
if (other.CompareTag("Player") && other.TryGetComponent(out JumpPad jumpPad))
{
// jumpPad.Launch(player);
}
if (other.CompareTag("Player") && other.TryGetComponent(out JumpPad jumpPad))
{
// jumpPad.Launch(player);
}
TheFatMike
TheFatMikeOP•2y ago
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
Accord
Accord•2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?