Hello, I am working on a mounted gun on one of my small boats.
This is an idea of how the welding is setup:
local tempWeld = Instance.new("Weld")
tempWeld.Parent = script.Parent.Parent.target
tempWeld.Part0 = script.Parent.Parent.target
tempWeld.Part1 = script.Parent.offset
local weldToBoat = Instance.new("Weld")
weldToBoat.Parent = script.Parent.Parent.Parent.ShipStructure.Parts.Model.TurrentLock.lock
weldToBoat.Part0 = weldToBoat.Parent
weldToBoat.Part1 = script.Parent.Parent.target
Below is the function that is called as W/A/S/D is being held down:
local tweenGoal = {}
local tween = nil
local init = true
function Turrent:Traverse(dir)
if init then
init = false
tweenGoal.C0 = self.Weld.C1 * CFrame.fromEulerAnglesXYZ(0, 0, 0)
end
if self.Boat.Alive == false then return end
if dir == "Up" then
tweenGoal.C0 = tweenGoal.C0 * CFrame.fromEulerAnglesXYZ(math.rad(2), 0, 0)
elseif dir == "Down" then
tweenGoal.C0 = tweenGoal.C0 * CFrame.fromEulerAnglesXYZ(math.rad(-2), 0, 0)
elseif dir == "Right" then
tweenGoal.C0 = tweenGoal.C0 * CFrame.fromEulerAnglesXYZ(0, math.rad(-2), 0)
elseif dir == "Left" then
tweenGoal.C0 = tweenGoal.C0 * CFrame.fromEulerAnglesXYZ(0, math.rad(2), 0)
end
local x, y, z = tweenGoal.C0:toEulerAnglesXYZ()
x = math.clamp(x, math.rad(-10), math.rad(30))
y = math.clamp(y, math.rad(-90), math.rad(90))
tweenGoal.C0 = CFrame.new(tweenGoal.C0.X, tweenGoal.C0.Y, tweenGoal.C0.Z) * CFrame.fromEulerAnglesXYZ(x, y, 0)
tween = TweenService:Create(self.Weld, self.tweenInfo, tweenGoal)
tween:Play()
end
The issue is it appears to be rotating globally:
How can I fix this so that it does not rotate on what appears to be the Z axis? Also, if anyone has a more efficient way of doing this, I am open to suggestions.