Hello everyone, I have a turret script, that for some reason glitches really bad.
Here’s what I mean:
https://gyazo.com/837fe11fc10fed992607e23420c9b210
The turret:
Script:
local rpart = workspace.TurretTest.RPart
local rpart2 = workspace.TurretTest.RPart2
local seat = workspace.TurretTest.VehicleSeat
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local UNIT_X = Vector3.new(1, 0, 0)
local UNIT_Y = Vector3.new(0, 1, 0)
local UNIT_Z = Vector3.new(0, 0, 1)
local IDENTITYCF = CFrame.new()
local function lookAt(eye, target, normalId)
local cf = IDENTITYCF
local forward = target - eye
if (forward:Dot(forward) == 0) then
return IDENTITYCF + eye
end
if (normalId and normalId ~= Enum.NormalId.Front) then
local face = Vector3.FromNormalId(normalId)
local axis = math.abs(face.z) >= 1 and UNIT_Y or UNIT_Z:Cross(face)
local theta = math.acos(-face.z)
cf = CFrame.fromAxisAngle(axis, theta)
end
forward = forward.Unit
if (math.abs(forward.y) >= 0.9999) then
return CFrame.fromMatrix(eye, -math.sign(forward.y) * UNIT_Z, UNIT_X) * cf
else
local right = forward:Cross(UNIT_Y).Unit
local up = right:Cross(forward).Unit
return CFrame.fromMatrix(eye, right, up) * cf
end
end
while wait() and seat.Occupant ~= nil do
local pos = mouse.Hit.Position
rpart.CFrame = lookAt(rpart.Position, pos)
rpart2.CFrame = lookAt(rpart2.Position, pos)
end