Hi, I’ve been writing a hoverbike system for the past couple of days and it’s been going well, but I’ve hit a problem: I can’t figure out how to make it account for the ground’s surface normal. What I have so far just makes it suddenly turn 180 seemingly at random:
local cameraLevelledVector = Vector3.new(camera.CFrame.LookVector.X,0,camera.CFrame.LookVector.Z)
local normal = sharedFunctions:GetSurfaceNormal()
local normalisedCFrame = normal and CFrame.new(Vector3.new(),normal)*CFrame.Angles(0,math.rad(-90),math.rad(90)) or CFrame.new()
bike.Chassis.BodyGyro.CFrame = normalisedCFrame*CFrame.new(Vector3.new(),cameraLevelledVector)*CFrame.Angles(0,math.rad(90),0)
Yes, I know it’s ugly. Don’t worry about GetSurfaceNormal()
- it basically returns a normal you’d get from a raycast, but it’s the mean average of 4 to smooth out the result. I already tried it with a single raycast to rule it out as a problem and the test yielded the same unpredictable results.
The extra set of angles on the end (0,-90,90)
is to account for the MeshPart’s weird orientation.
For reference, here’s the code without my attempt at accounting for ground normal, which works fine (albeit doesn’t adapt to the terrain):
local cameraLevelledVector = Vector3.new(camera.CFrame.LookVector.X,0,camera.CFrame.LookVector.Z)
bike.Chassis.BodyGyro.CFrame = CFrame.new(Vector3.new(),cameraLevelledVector)*CFrame.Angles(0,math.rad(90),0)
What am I doing wrong? Why does my bike suddenly turn about the Y axis 180 degrees? I’m for all intents and purposes new to CFrames, as historically they’ve given me nothing but grief, so anything you all could tell me would help a lot. If I’ve missed anything out, please ask.
Thanks!