For characters with capsule component? In Character Tick, Do a line trace downward and check how far the line trace goes :
Get the location of the character : Vector “location” = Character->GetActorLocation
Now you know where the character centerpoint is is
Get the character capsule half height : float halfheight = Character->GetCapsuleComponent->CapsuleHalfHeight
Now you know how far from your location you need to go down to get to the lowest point of your character
break location vector, make a new vector “lowestPoint”, keep the x and y coordinates from location Vector, and subtract halfheight from your location vector z vaue before plugging it in the z value of the “lowestPoint” vvector.
Now you have the lowest point of your characters hit box. Now all we need to do is do a linetrace downwards from your lowestPoint vector
Start: lowestPoint vector
End : lowestPointVector but subtract, say, 10000 (for 100 meters) from the z value, trace channel Visibility.
Now you have a LineTraceByChannel that every frame goes from the lowest point of the character down for 100 meters and checks if something is there.
in your return value,you now have
- a boolean “Return value”, which is true if something is less than 100 meters below you,
- and an “out hit” struct, which,if the “Return value” boolean is true, you can split, so you can get the “out hit distance” float, which, if you are less than 100 meters in the air, gives you exactly how far above the ground you are
Note 1 : If you have performance problems or you know you always are less than 100 meters above ground (depends on level design), you can of course reduce the number in step 4.
Note 2 : Depending on how you make your game, you might want to consider creating a separate line trace channel in your project settings->collision that, instead of visibility, checks only for static world things (aka the ground). Tracing by visibility channel might give you wrong jump heights if something like a bird or a bullet or a leaf is exactly below you (and thus blocking the visibility trace).
EDIT: Oh yea, before you do all that, you can check with “character -> Is Falling” whether the character is in the air or not. You only have to do all that above if the character is in the air, no need to waste ressources when you are on the ground (in which case Is Falling is false).