Line Trace to NOT Follow Ball Rotation

I’m trying to cast a line trace from center of ball to floor, however, when the ball rotates so does the line. How do I cast a line that follows the ball around but not the rotation of the ball?

image

Can you show the code? ( and probably the component hierarchy ) :slight_smile:

a line trace needs at the very least 3 things: a Start Point, an End Point, and a criteria to check against; with just those there is no “direction vector” involved in a LineTrace().

I am going to guess that you want to know ‘how far from the ground your “ball” object is?’
so we want to shot the line from the StartPoint (probably the center of the ball though this can create some ambiguity if you want precision), and the thing to check for is probably going to be “WorldStatic” because we want “the ground” (if we want something else there could be issues).
the tricky part is plugging in the “EndPoint” for the trace well presuming you have not fundamentally changed how the engine works (or we aren’t dealing with rotating gravity). Unreal uses +Z-Up so we can get this a few different ways, but most will start with StartPoint which will just be the GetWorldLocation() probably from the Actor’s Pointer (for blueprints the term is reference but in that case they are “the same”) this will be the StartPoint (unless you want increased precision in the distance from ground)

  • you can subtract a really big number (like 1,000 or some other number that is far beyond the expected “max height” the object will get to) from the Z-component of the Location vector and use that as the EndPoint (this can have a bad result if the Object is bellow “the ground” but I will partially ignore this situation at the moment) then out of the HitResult we can get the Hit.Location which we can then use to get the distance (or the distance squared if that is acceptable for better performance in saving the square root)
  • “slowly” increment the distance being subtracted from the Z-Component of the Location vector for the EndPoint in a for loop; this way we “already know” a smaller range for the distance (if we increment the amount being subtracted from the Z-component of the StartPointby 10 and we have done it 3 times before we found “the ground” then we know it is somewhere between 20 and 30 units above the ground, and we might not even “need” the distance check. (this method can be more computationally expensive, and you will want to put a cap to the number of iterations)
  • the final option in my mind is probably the dirtiest where if we know where the ground is (like if the ground shall always be at value Z==0 then we can just read the Z-Component of the Location and that is the height above “the ground” (this implementation falls down if “the ground” is not at Z==0)

the biggest issue with using the center point of the object to determine how far away from the ground is; the only time Z==0 will be true (regardless of floating point Approximations) will be when the object is inside the ground, not on the ground but inside it. (this is one of the reasons why like the built in Mannequins have their origins between their feet and not in their center) so to account for this you will need to not exactly use the “center of the ball” but rather subtract the radius of the ball from the Z-component of the “center of the ball” then use it.

If these objects have MovementComponents then you can use IsFalling() (this really just checks the sign on Z-component of the direction of motion) as an object that is moving upwards probably doesn’t need to be checking where the ground is.

all 3 of these approaches should work with either blueprints of C++ with any of the built in Line Trace Single functions. though I am assuming that +Z-Up is still in effect.

trick

3 Likes

This worked and is so funny, I had this exact code at one point, but didn’t add the negative value on the Z axis. I’m new to unreal and this is going to help me out tremendously. Thank you so much!

Thank you all actually who replied to this post. You’ve given me a ton of insight!

2 Likes