TraceRes.HitNormal and Angles

So far, after consulting several other Lua scripters, I have been unable to resolve this problem.

The premise is I am attempting to create a vehicle that will travel along a straight path while floating parallel to the surface below it.

So, it works. Kind of. I can get the entity to float parallel to the surface below it, but I run into a problem in that the HitNormal function doesn’t seem to be consistent yaw-wise. For example, I can go from one section of track to the next and turn 90 or 180 degrees for no reason.

If I try to go in a loop, I’ll be moving sideways, backwards, and forwards before I can finish.

Has anyone else had this problem, or does anyone have a solution? (I’m just trying to find a way to make the angles consistent across multiple surfaces)

Totally ignore the yaw.

proper_hit_normal = trace.HitNormal:Angle()
proper_hit_normal.y = my_ship:GetAngles().y
proper_hit_normal = proper_hit_normal:Forward()

Should work.

Hmm. Well, I tried that, but for whatever reason whenever I accelerate onto a surface sloped upward it just pitches into the ground (that is, only when I change the yaw). Which doesn’t make much sense.

edit Well, it kind of works when I go backwards until I go beyond a 90 degree angle and then it spazzes out.

We could divulge into a mass of vector mathematics that would allow you to calculate the normal vector relative to the angle between the two, which allows you to calculate what the yaw would be at that angle.

Or… you can just change how it works so that you do four traces downwards (one in each corner) and apply forces from the point the trace is made (ApplyForceOffset) so that the trace points will then be at the desired height.