Download

[HELP] While loop to check for anything above the character while crouching

I’m trying to make it so if the player tries to uncrouch it will start a loop that will uncrouch the player when nothing is above them.
Heres the blueprint with the loop checking if anything is above them:

And heres the blueprint that makes the player crouch:

Any help is appreciated and if you have any critiques of my code please say.

Found this and it does what I want but crashes the game because of how many times it loops is there any other way to do this?

I want the player to stand up automatically when theres nothing above them as long as they aren’t holding the crouch button

I don’t know how you scripted you character, but you shouldn’t start at the actor location or the skeletal mesh component of the actor?

If i used something like first person or third person template, i would get the world location of the PlayerSkeletalMesh as a start
And for the End use the Start Vector + Your Desired distance in Z

Loop is a bit expensive, even more in blueprints. You really need that the player uncrouch automatically? If not, i’d say that make the player press the button and try to uncrouch, otherwise, a better option would be “Set Timer by Event” and mark the loop option. You can assign a ticking time (you could use 0.2 seconds for example and make that check five times per second, if you need a faster response use a lower value for that time).

Edit: I had to come here because i forgot to tell something very important.
When you Set Timer by Event, there’s a return value (blue). You need to use it (Usually promove to a variable for easy reference) to “Clear and Invalidate Timer by Handle” otherwise there’s nothing stoping this loop. So even when you are uncrouched, the loop will still be running.

An easy way to think about it. It’s just like a While loop
You start this loop and While it’s not Cleared and invalidated, this will keep running.

Your loop is going to infinitely fire the moment you check for crouch.

The way you’re setting your Can Un-crouch bool is the culprit here. If the trace is returning hits, you don’t need to be setting the bool; it’s already false if we’re running the loop.

Then, you’re setting it true and then false again. So at no point can you exit your loop because when the loop comes to the end of its execution flow, the bool will always be false.

Set Can Un-crouch to be the logical NOT of your trace return value. You don’t need a branch, you just need Can Un-crouch to be the opposite of your trace return value. So if it returns true, your bool is false. If it returns false, your bool is true. Easy.