x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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

I'm working on a game where the character can glide from high places. I've been thinking about the controls and how "Glide mode" would be activated, and the behavior I'd like is when the player jumps off of a ledge, the engine knows that the 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 the distance from the ground a number of units in front of the player equal to the lateral distance of the player's jump would give me the 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!

Product Version: Not Selected
Tags:
more ▼

asked Nov 19 '14 at 04:21 PM in Blueprint Scripting

avatar image

Valkrist
1 3 4 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hey Valkrist,

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

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

alt text

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

Then on Event Tick, I used Line Trace by Channel. I get the LineTraceStart component's World Location (you might need to turn Context Sensitive off to find this in 4.5.1) for the Start input, and for the End input I subtract a vector value for the distance I want the 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 the 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 the Out Hit and Break Hit Result to get the Location of the trace hit. Subtract that from the LineTraceStart's World Location to get the difference between the two locations, Break Vector to get the Z value of that difference, and there's your distance variable!

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

distancetrace.png (224.6 kB)
more ▼

answered Nov 19 '14 at 11:58 PM

avatar image Valkrist Nov 20 '14 at 03:13 PM

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.

avatar image Ben Halliday STAFF Nov 20 '14 at 07:23 PM

No, line traces have a negligible impact on performance. In fact, if you use the CharacterMovement component, there's one built in that detects whether the 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 the 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 the distance, you can set up a function for all of this and use a timer to call the function every few seconds, instead.

avatar image Valkrist Nov 24 '14 at 03:50 PM

Great, thanks again for the help. I think what I'll do is instead of having the raycasts check every tick is implement the check into the jump. So, when the player chooses to jump, the check will be made, and the 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 the player would land from the origin of the jump depending on how fast they are traveling. I think I can figure that out, though.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question