How can I limit the height that the player can fly vertically?

I have edited the FPS player blueprint to be able to fly. WASD to move around, Space to move up, and left Ctrl to move downward.

This allows the player to traverse upward indefinitely. I do not have the option to use an invisible wall as a ceiling, but I need to limit the height the player can get to based on what is underneath them. I assume I need to use some sort of raytrace, but I’m not sure.

To better explain, I want the player to move upwards when i press the spacebar, but once a defined height is reached above whatever is underneath them, they can’t go any higher.

Any help on this would be wonderful.

Thank you.

Could you not simply compare the actor’s current world position in the up axis and prevent elevation in the event that the current height is greater than or equal to a height limit?

e.g
if (GetActorLocation().Z >= MaxHeight) {
//prevent elevation/input from spacebar
}

Of course, you may find you want to use a raycast anyway for other purposes in the future, so maybe a raycast would suit you better.

Then you could do something similar, but just stop input when the ray’s distance exceeds the max height.
In this case, you would also need to ensure that your ray is actually hitting the ground, and not being intercepted by another object (say, another flying character), else you would effectively be able to double your height limit value. Line trace by channel could help you out here.

I suggest you look at this post: How do I detect distance between player and ground x units in front of player - Programming & Scripting - Unreal Engine Forums

The blueprint in this link helped me out but I ended at a different solution. My use is to have a the player fly at a designated height all the time, and I simply split the vector when adding movement input and split the forward and right vectors going into that node. I then used an if statement to check whether the actor’s z location was equal to my desired height and swept the actor location back to that z value if not.

edit: Reworked this to allow change of terrain elevation (only worked on flat plane where we had constant height before). I am now setting the base eye height for my camera at the desired ‘fly’ height (a variable I am adjusting in-game for higher aerial shots) and letting the capsule component keep track of the ground to keep me at the right elevation above it. An interesting side note: I had the pawn’s control rotation determine the movement input vector, which was causing me to slow to a halt as I changed from looking forward to looking down. Was kind of a cool effect that I opted to replace with an arrow component so that I’d have constant speed.