Servo turret breaks when the car moves

So when my car with a turret is in its start position, the turret rotates perfectly, but when i move my car, the turret rotation just breaks, the turret just doesn’t follow the mouse anymore and just rotates with offset.

Local script:

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Mouse = Player:GetMouse()

local Events = RS:WaitForChild("Events")
local ATGMValue = script:WaitForChild("ATGMValue")
local VehicleValue = script:WaitForChild("VehicleValue")
local ATGM = ATGMValue.Value

local MoveTurretEvent = Events:WaitForChild("MoveTurret")
local SitEvent = Events:WaitForChild("PlayerSitting")
local FireEvent = Events:WaitForChild("Fire")
local MousePosEvent = Events:WaitForChild("MousePos")

local Camera = workspace.CurrentCamera

local IsSitting = false
local IsAiming = false
local debounce = false

Mouse.TargetFilter = VehicleValue.Value

SitEvent.OnClientEvent:Connect(function(Occupant)
	if Occupant ~= nil then
		Humanoid:UnequipTools()
		IsSitting = true
	elseif Occupant == nil then
		IsAiming = false
		IsSitting = false
		ExitCamera()
	end
end)

Mouse.Button1Down:Connect(function()
	if IsSitting == true and IsAiming == true then
		FireEvent:FireServer()
	end
end)

Mouse.WheelForward:Connect(function()
	if IsAiming == true and Camera.FieldOfView > 15 then
		Camera.FieldOfView = Camera.FieldOfView - 5
	end
end)

Mouse.WheelBackward:Connect(function()
	if IsAiming == true and Camera.FieldOfView < 60 then
		Camera.FieldOfView = Camera.FieldOfView + 5
	end
end)

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.X and not gameProcessedEvent and IsSitting == true then
		if IsAiming == false then
			IsAiming = true
			EnterCamera()
		elseif IsAiming == true then
			IsAiming = false
			ExitCamera()
		end
	end
end)

function ExitCamera()
	local CamPart = ATGM:WaitForChild("CamPart")
	IsAiming = false
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	Camera.FieldOfView = 70
	Player.CameraMode = Enum.CameraMode.Classic
	UIS.MouseDeltaSensitivity = 1
	UIS.MouseBehavior = Enum.MouseBehavior.Default
	UIS.MouseIconEnabled = true
end

function EnterCamera()
	local CamPart = ATGM:WaitForChild("CamPart")
	IsAiming = true
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = CamPart
	Camera.FieldOfView = 60
	Player.CameraMode = Enum.CameraMode.LockFirstPerson
	UIS.MouseDeltaSensitivity = 0.06
	UIS.MouseIconEnabled = true
	UIS.MouseBehavior = Enum.MouseBehavior.Default
end


local function MoveTurret()
	if IsAiming == true then
		local HHinge = ATGM:WaitForChild("HHinge")
		local VHinge = ATGM:WaitForChild("VHinge")
		local Delta1 = Mouse.Hit.Position - VHinge.CFrame.Position
		local Delta2 = Mouse.Hit.Position - HHinge.CFrame.Position
		local Angle1 = math.atan2(Delta1.Z,-Delta1.X)
		local Angle2 = math.atan2(Delta2.Y,-Delta2.X)
		print(math.deg(Angle1))
		MoveTurretEvent:FireServer(Angle1,Angle2)
	elseif IsAiming == false then
		Camera.CameraType = Enum.CameraType.Custom
		Camera.CameraSubject = Player.Character.Humanoid
	end
end

RunService.RenderStepped:Connect(MoveTurret)
RunService.RenderStepped:Connect(MoveTurret)

Server script:

local PlayerSitting

local ATGM = script.Parent.Parent

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events")

local PlayerSittingEvent = Events:WaitForChild("PlayerSitting")
local MoveTurretEvent = Events:WaitForChild("MoveTurret")
local FireEvent = Events:WaitForChild("Fire")

local Missile = ATGM:WaitForChild("Missile")
local FireSound = ATGM:WaitForChild("HRotate"):WaitForChild("Tube"):WaitForChild('FireSound')
local MissileValue = script.Parent:WaitForChild("Missile")

local GUI = script.Parent:WaitForChild("ATGMUI")

local VHinge = ATGM:WaitForChild("VHinge")
local HHinge = ATGM:WaitForChild("HHinge")

local debounce = false

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.Occupant ~= nil then
		PlayerSitting = script.Parent.Occupant.Parent
		local Player = Players:GetPlayerFromCharacter(PlayerSitting)
		VHinge.HingeConstraint.TargetAngle = 0
		HHinge.HingeConstraint.TargetAngle = 0
		if not Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI") then
			local GUIClone = GUI:Clone()
			GUIClone.Parent = Player:WaitForChild("PlayerGui")
		end
		PlayerSittingEvent:FireClient(Player,script.Parent.Occupant)
	elseif script.Parent.Occupant == nil then
		local Player = Players:GetPlayerFromCharacter(PlayerSitting)
		PlayerSittingEvent:FireClient(Player,script.Parent.Occupant)
		MissileValue.Value = nil
		if Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI") then
			Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI"):Destroy()
		end
	end
end)

MoveTurretEvent.OnServerEvent:Connect(function(player,Angle1,Angle2)
	VHinge.HingeConstraint.TargetAngle = math.deg(Angle1)
	HHinge.HingeConstraint.TargetAngle = math.deg(Angle2)
end)

FireEvent.OnServerEvent:Connect(function(player)
	if debounce == false and MissileValue.Value == nil then
		debounce = true
		Missile.Transparency = 1
		FireSound:Play()
		MissileFire()
		task.wait(5)
		debounce = false
	end
end)

function MissileFire()
	local MissileClone = Missile:Clone()
	if MissileClone then
		MissileClone.Transparency = 0
		MissileClone.Parent = script.Parent.Parent.ATGMMissile
		MissileValue.Value = MissileClone
		MissileClone.CFrame = Missile.CFrame * CFrame.new(0,0,20)
		MissileClone:WaitForChild("ControlScript").Enabled = true
		MissileClone:WaitForChild("Fire").Enabled = true
		MissileClone:WaitForChild("Flare").Enabled = true
		MissileClone:WaitForChild("PointLight").Enabled = true
		MissileClone:WaitForChild("Smoke").Enabled = true
		MissileClone:WaitForChild("FlySound").Playing = true
		MissileClone:WaitForChild("ExplosionScript").Enabled = true
	end
end


image

image

1 Like

try resetting the orientation of the turret to the original orientation when you get in the seat

1 Like

I tried setting the TargetAngle on the servo to 0, doesn’t work sadly

try seeing what properties the turret has originally like the orientations and such and then setting those properties back, I don’t know exactly how to explain it but basically just reset the turret by messing around to see what works

If you don’t mind, could you provide the full code or upload a reproducible model? It would help understand the issue

Alright, i edited the post it has all the details now

Thanks! What does happen when you try getting off and on the turret without moving the car? Does the turret work properly as expected?

Yeah, it works without any issues, i am sure it’s because i’m moving the car

Your angle calc method should have to object space

1 Like

Since you are calculating your atan2 operation in world-space, orientation of the hinge(car) is not considered during calculation. As @dthecoolest mentioned above, changing your atan2 operation object-spaced will solve the issue!

As you can see in the image above, when car is facing -Z(car on the left), the angle(green) is properly applied thus turret’s orientation is set accordingly. But once the car’s orientation is changed(car on the right), since turret’s TargetAngle is not world-spaced, TargetAngle is set to the angle(green), not 0 (even though you are looking forward in terms of the car) resulting in turret to seem oriented weirdly.

2 Likes

I can’t try that right now, i need to sleep but tomorrow i can, thank you guys for helping

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.