Hello! I just started working on a helicopter and I ran into a problem, the turning was normal before adding a forward tilt to the helicopter and now it does this:
After Adding the forward tilt:
Before adding the forward tilt:
Here is my code:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Player:Player = Players.LocalPlayer
local Character = Player.Character
local Helicopter:Model = script.Helicopter.Value
local Primary = Helicopter.PrimaryPart
local LinearVelocity:LinearVelocity = Primary.LinearVelocity
local VectorForce:VectorForce = Primary.VectorForce
local AlignOrientation:AlignOrientation = Primary.AlignOrientation
local ControlModule = require(Player.PlayerScripts.PlayerModule.ControlModule)
local MaxSpeed = 50
local AccelerationSpeed = 2.5
local CurrentSpeed = 0
local function GetMass()
local TotalMass = 0
for _, v in Helicopter:GetDescendants() do
if v:IsA("BasePart") then
TotalMass += v:GetMass()
end
end
for _, v in Character:GetDescendants() do
if v:IsA("BasePart") then
TotalMass += v:GetMass()
end
end
return TotalMass
end
RunService.RenderStepped:Connect(function(DeltaTime)
local MoveVector = ControlModule:GetMoveVector()
VectorForce.Force = Vector3.new(0, workspace.Gravity * GetMass(), 0)
local SpeedGoal = math.abs(MoveVector.Z) * MaxSpeed
CurrentSpeed = CurrentSpeed + (SpeedGoal - CurrentSpeed) * math.min(DeltaTime * AccelerationSpeed, 1)
local NormalizedLookVector = Primary.CFrame.LookVector.Unit
LinearVelocity.VectorVelocity = Vector3.new(NormalizedLookVector.X, 0, NormalizedLookVector.Z) * -(MoveVector.Z * CurrentSpeed)
local AlignOrientationCFrame = AlignOrientation.CFrame * CFrame.Angles(math.rad(10) * -MoveVector.Z, math.rad(-MoveVector.X), 0)
local X, Y, Z = AlignOrientationCFrame:ToOrientation()
local ClampedX = math.rad((math.clamp(math.deg(X), -10, 10)))
if MoveVector.Z == 0 then
ClampedX = 0
end
AlignOrientation.CFrame = CFrame.Angles(ClampedX, Y, 0)
end)
Primary.CFrame = Primary.CFrame + Vector3.new(0, 10, 0)
Correct me if I’m wrong but it looks like the Y axis is rotated along with the AlignOrientation. So when you tilt forward the Y axis points straight up from the tilt as it should
What you actually want to do is rotate around a neutral upwards direction not impacted by the tilt of the helicopter.
I’d probably move the math.rad(-MoveVector.X) portion out of the CFrame.Angles and apply it directly when you set the AlignOrientation’s CFrame to see if that fixes it.
Hmm a hacky fix I usually use for these issues is just have a variable angleY and do angleY += math.rad(-MoveVector.X * 10) then use angleY instead. Of course make sure the variable is made outside of the RenderStepped event otherwise it will reassign everytime and cause the same issue
SO I think I found a solution. What worked for me in my own little test place was to utilize CFrame.fromAxisAngle which allows us to specify the axis of rotation. So if I am not completely mistaken you should be able to apply this like so