Aligning car to ground incline


So I’ve made a vehicle controller using body movers and raycasting, however I can’t quite seem to be able to set the rotation of the car to the terrain below me.

I have tried to make a bootleg incline script using cframes however it doesn’t seem to be very accurate once you turn on inclines (As shown down below)


The Code
local function Angle(p1, p2)
    Part.CFrame = CFrame.new(p1, p2)
    return Part.Orientation
end


local FL, FR, BL, BR = Rotations["FL"], Rotations["FR"], Rotations["BL"], Rotations["BR"] 
local A1 = Angle(FL, BL)
local A2 = Angle(FR, BR)
local A3 = Angle(BL, BR)
local A4 = Angle(FL, FR)
local Or = (A1 + A2 + A3 + A4)/2
local Cframe = CFrame.new() * CFrame.fromEulerAnglesYXZ(math.rad(-math.abs(Or.X)), 0, math.rad(-math.abs(Or.Z)))

FL, FR, BL, BR are equal to 4 rays cast down from each corner of the car.
Obviously this is a very bad way of doing things however I couldn’t find a solution I could understand so I decided to make something like this.


Video


I’ve looked for a couple solutions for this but I’m starting to run out of things to search to find what I need and I’m hoping someone can help me with this issue.

I have a chassis casting 4 rays from each corner and can retrieve the data for each individual one. I’m trying to get the incline by averaging each position instead of using normals if that gives you any idea how to fix it…

2 Likes