How do I detect distance between player and ground x units in front of player

I’m working on a game where character can glide from high places. I’ve been thinking about controls and how “Glide mode” would be activated, and behavior I’d like is when player jumps off of a ledge, engine knows that player would be falling for a certain distance and automatically activate a different jump animation to transition into glide mode. I was thinking that having a variable that tracked distance from ground a number of units in front of player equal to lateral distance of player’s jump would give me information I need to do this, but I have no idea how to blueprint this. I’m very new to UE4 in general, so please be specific. Thanks very much!

Hey ,

easiest way to do this would be to do a line trace straight down from a point in front of player character. If you get hit location and subtract it from source of line trace, and then get difference in Z, you’ll have your distance to ground as a float.

Here’s an image of what it might look like in your character Blueprint:

I created a Sphere Component in Components tab and called it LineTraceStart. On Begin Play, it takes that component and Sets Relative Position (it’s location relative to Root, in this case Capsule Component) to vector Distance From Player. You can just as easily position component in Components tab to distance you would like it in front of player, but I set it up this way so that I can adjust DistanceFromPlayer vector variable during play if I want. For this, though, I simply set default value of vector variable to 0,0,300 to place it a bit in front of character.

Then on Event Tick, I used Line Trace by Channel. I get LineTraceStart component’s World Location (you might need to turn Context Sensitive off to find this in 4.5.1) for Start input, and for End input I subtract a vector value for distance I want trace to go. For this, I used a vector variable again and set its default value to 0,0,1000 to give it a nice long trace, but you may need to set this higher depending on how high your character will be.

You can adjust Trace Channel later, if you want, but Visibility is good to start with. Later, you may want to create a custom Trace Channel (Project Settings > Collision) and set it to only see certain types of actors (so it doesn’t calculate clouds or trees for distance).

Then I used Out Hit and Break Hit Result to get Location of trace hit. Subtract that from LineTraceStart’s World Location to get difference between two locations, Break Vector to get Z value of that difference, and there’s your distance variable!

Hope that helps! Let me know if you have any questions.

7 Likes

This is fantastic and exactly what I was looking for. Thank you very much. You’ve got credit for answering. I do have a question about this solution, though. It seems there is a lot going on for something so simple. Are raycasts significant hits on performance? I know in some older engines, including UDK I think, ray tracing had to be used sparingly.

No, line traces have a negligible impact on performance. In fact, if you use CharacterMovement component, there’s one built in that detects whether ground is at a walkable angle every time your character moves! You don’t have to worry about that too much.

In general, you want to keep as many things out of Tick event as you can, because that can get overloaded quickly, but it’s okay in cases like this where you actually need a continual update. If you don’t need that frequent an update for distance, you can set up a function for all of this and use a timer to call function every few seconds, instead.

2 Likes

Great, thanks again for help. I think what I’ll do is instead of having raycasts check every tick is implement check into jump. So, when player chooses to jump, check will be made, and animation and appropriate movement will be determined right then and there. Also, I hadn’t considered this before, but I’ll also have to add functionality to determine how far player would land from origin of jump depending on how fast they are traveling. I think I can figure that out, though.

1 Like