✓ Accepted answer
The following script is from my prototype turret that uses constaints to move. It had 2 pitch and 1 yaw constraints and i had it follow a part in the workspace. You can hack it to work with the mouse. Since you have the mouse hooked up, just swap out the Firepoint varable and tweek the setting in RotatetoTarget() function to read:
function RotatetoTarget(target, enginePos)
local targetPos = target.Position
local dir = (targetPos - enginePos).unit
to
function RotatetoTarget(target, enginePos)
local targetPos = target
local dir = (targetPos - enginePos).unit
And call the function by:
while true do
local FirePoint = workspace.FirePoint
local DirCF = RotatetoTarget(FirePoint, Brain.Position)
local Rot = getOrientation(DirCF)
while true do
local FirePoint = Mouse.CFrame.Position
local DirCF = RotatetoTarget(FirePoint, Brain.Position)
local Rot = getOrientation(DirCF)
Code:
local Self = script.Parent
local Brain = script.Parent.Brain
local YawMotor = script.Parent["Motors"].Yaw
local PitchMotors = {}
for i=1,2 do
PitchMotors[i] = script.Parent["Motors"]["Pitch"..i]
end
function RotatetoTarget(target, enginePos)
local targetPos = target.Position
local dir = (targetPos - enginePos).unit
local pos = enginePos + dir
return CFrame.new(pos, pos+dir)
end
local function getOrientation(CF)
local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = CF:components()
local Y = math.atan2(m02, m22)*(180/math.pi)+180
local X = math.asin(-m12)*(180/math.pi)+180
local Z = math.atan2(m10, m11)*(180/math.pi)+180
return Vector3.new(X,Y,Z)
end
local t = 0
while true do
local FirePoint = workspace.FirePoint
local DirCF = RotatetoTarget(FirePoint, Brain.Position)
local Rot = getOrientation(DirCF)
local CanIFire = true
YawMotor.TargetAngle = Rot.Y
for k,v in pairs(PitchMotors) do
v.TargetAngle = Rot.X -180
end
wait()
end
hope this helps!
1 comment
Thanks for helping! It's been a while, and I will go back to the turret project eventually. I'm working on something else right now, so when I go back I will read this post again.