I have a plane set up like this with a gimbal on the bottom:
What I want to do is I want to keep the bomb platform flat to the ground, but I am not bothered about the yaw, only that it is flat to the ground.
In order to do this I have a made a gimbal that is made up of two servos, allowing the platform to be rotated in the pitch and roll axes relative to the plane:
I thought I could get the pitch and roll of the plane, and rotate the servos by the same amount in the opposite direction to cancel it out, and thereby making it flat to the ground, with only some yaw left which I’m not bothered about.
However, I am getting some strange offset that I really can’t figure out how to resolve:
This is the code I have made that simulates the plane moving, and controls the servos on the gimbal:
local model = script.Parent
local gimbal = model.Gimbal
local plane = model.Plane
local roll = gimbal.RollParent.Roll
local pitch = gimbal.PitchParent.Pitch
local minAng = -30
local maxAng = 30
local function genAngle()
return math.random(minAng, maxAng) * math.pi/180
end
wait(1)
-- Simulate plane
coroutine.wrap(function()
local target = CFrame.new(plane.CFrame.Position) * CFrame.Angles(genAngle(), genAngle(), genAngle())
local steps = 50
local stp = 0
while wait() do
stp = stp + 1
if stp >= 50 then
target = CFrame.new(plane.CFrame.Position) * CFrame.Angles(genAngle(), genAngle(), genAngle())
stp = 0
end
plane.CFrame = plane.CFrame:Lerp(target, 1/50)
end
end)()
-- Control gimbal
coroutine.wrap(function()
while wait() do
local x, y, z = plane.CFrame:ToEulerAnglesXYZ()
roll.TargetAngle = -z * 180/math.pi
pitch.TargetAngle = x * 180/math.pi
end
end)()