-
I want that my Turret LeftRight Motor6D rotates correctly according to the Rotation of the Camera
-
When the Turret Base gets rotated, the angle is not correct
-
I have tried many different approaches, like rotating the cameraCF with the Y Rot. of the Base Part
You can see things i tried allready as comments, i am new to all these angle calculation things and advanced CFrame stuff.
This is only a code Snippet:
if cameraCF and seat.Occupant then
--[[
local angle = -Vector3.xAxis:Angle(mouseCF.Position)
local _,_,currentUpDownAngle = Motors.UpDown.C0:ToEulerAnglesXYZ()
local _,currentLeftRightAngle,_ = Motors.LeftRight.C0:ToEulerAnglesXYZ()
if currentLeftRightAngle < maxLeftRightAngle and currentLeftRightAngle > -maxLeftRightAngle then
if angle <= currentLeftRightAngle then
Motors.LeftRight.C0 = Motors.LeftRight.C0 * CFrame.Angles(0, radSpeed , 0)
elseif angle >= currentLeftRightAngle then
Motors.LeftRight.C0 = Motors.LeftRight.C0 * CFrame.Angles(0, -radSpeed , 0)
end
end
--]]
--local axis = Vector3.xAxis.Unit
local primPartCF = script.Parent.PrimaryPart.CFrame
local lookVector = (cameraCF ).LookVector --* (primPartCF.RightVector + (primPartCF.LookVector) * Vector3.new(1,0,1))
local upDownLookvector = -(cameraCF * CFrame.Angles(math.rad(10), 0,0) ).LookVector * Vector3.new(1,1,0)
--local angle = math.acos(lookVector:Dot(axis))
--if lookVector.Z < 0 then
-- angle = -angle
--end
local _,baseRot,_ = primPartCF:ToEulerAnglesXYZ()
--local angle = math.clamp(math.atan2(lookVector.Z, lookVector.X), -maxLeftRightAngle - baseRot , maxLeftRightAngle + baseRot )
local angle = math.atan2(lookVector.Z, lookVector.X)
--calculate position between z and x
local upDownAngle = math.clamp( math.sin( upDownLookvector.Y ), maxDownAngle, maxUpAngle)
local moveThreshold = 0.01
local _,currentLeftRightAngle,_ = Motors.LeftRight.C0:ToEulerAnglesXYZ()
local _,_,currentUpDownAngle = Motors.UpDown.C0:ToEulerAnglesXYZ()
if math.abs(currentLeftRightAngle - angle) >= moveThreshold then
Motors.LeftRight.C0 = CFrame.new(Vector3.new(0,-0.5,0)) * CFrame.Angles(0,angle,0)
end
if math.abs(currentUpDownAngle - upDownAngle) >= moveThreshold then
Motors.UpDown.C0 = CFrame.new(Vector3.new(0,-0.75,0)) * CFrame.Angles(0,0,upDownAngle)
end
end