How do i make a tank turret that turns the direction the players mouse moves

basically im trying to make a tank turret that turns when the mouse is moved in a direction and what i need to know is just how i can get the direction the mouse moves similar to like multicrew tank combat 4 turrets when you press X it goes into mouse movement mode where it does what i describe. i tried mousedelta techniques but it wouldn’t work once the mouse reached the edge of the screen, any help?

1 Like

There was a discussion about this just a couple of weeks back (and a few other times before that) where the math is discussed. The examples files in this thread assume the turret is connected to the tank with a HingeConstraint, which is not the only way, but how most of us do it since it replicates smoothly to everyone, unlike setting part CFrames.

1 Like

thank you for the help but i found the solution from ferrarico’s tank script and just looking around. (MTC4 uses the same model just heavily modified)

for anyone wondering, here’s the script in question, credit to the legen.

local VertBase = Vehicle.Chassi.CommMG.Hinge
local VertRef = Vehicle.Chassi.Cupola.MGBase
local Cupola = Vehicle.Chassi.Hatches.CupolaHatch

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
Camera.CameraSubject = Player.Character.Humanoid
local Aiming = false
local SensLimit = 5
local angleLimit = 20
local currentAngle = VertBase.Orientation.X-VertRef.Orientation.X
local event = game.ReplicatedStorage.TankGunsFolder:WaitForChild("TurretRotateEvent")
local RunService = game:GetService("RunService")

mouse.TargetFilter = Vehicle

local function EnterCamera()
	Aiming = true
	Camera.CameraType = Enum.CameraType.Scriptable
	if Cupola.IsOpen.Value == true then
		AimPart = Vehicle.Chassi.CommMG.GunScope
	elseif Cupola.IsOpen.Value == false then
		AimPart = Vehicle.Chassi.Cupola.InteriorScope
	end
	Camera.CoordinateFrame = AimPart.CFrame
	Camera.FieldOfView = 60
	UserInputService.MouseDeltaSensitivity = 0.06
	UserInputService.MouseIconEnabled = false
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end

local function ExitCamera()
	Aiming = false
	Camera.CameraType = Enum.CameraType.Custom
	Camera.FieldOfView = 70
	Camera.CameraSubject = Player.Character.Humanoid
	UserInputService.MouseDeltaSensitivity = 1
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	UserInputService.MouseIconEnabled = true
end

DriverSeat.Changed:connect(function(property)
	if property ~= 'Occupant' then return end
	local occupant = DriverSeat.Occupant
	if not occupant then
		ExitCamera()
		script.Parent.GuiRemover.RemoveEvent:FireServer()
	end
end)

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F and not gameProcessedEvent then
		if Aiming == false then 
			EnterCamera()
		elseif Aiming == true then 
			ExitCamera()
		end
	end
end)

local function MoveTurret()
	if Aiming then
		Camera.CoordinateFrame = AimPart.CFrame
		UserInputService.MouseDeltaSensitivity = 0.06
		local delta = UserInputService:GetMouseDelta()
		local deltaX = delta.X
		local deltaY = delta.Y
		if delta.X > SensLimit then
			deltaX = SensLimit
		elseif delta.X < -SensLimit then
			deltaX = -SensLimit
		end
		if delta.Y > SensLimit then
			deltaY = SensLimit
		elseif delta.Y < -SensLimit then
			deltaY = -SensLimit
		end
		currentAngle = VertBase.Orientation.X-VertRef.Orientation.X
		if currentAngle > 0 then
			if currentAngle+deltaY > angleLimit then
				deltaY = 0
			end
		elseif currentAngle < 0 then
			if currentAngle+deltaY < -angleLimit then
				deltaY = 0
			end
		end
		HorBase.BTWeld.C0 = HorBase.BTWeld.C0 * CFrame.Angles(math.rad(-deltaX),0,0)
		VertBase.BTWeld.C0 = VertBase.BTWeld.C0 * CFrame.Angles(math.rad(-deltaY/2),0,0)
		event:FireServer(HorBase,VertBase,HorBase.BTWeld.C0,VertBase.BTWeld.C0)
	else
		Camera.CameraType = Enum.CameraType.Custom
		Camera.CameraSubject = Player.Character.Humanoid
	end
end

RunService.RenderStepped:Connect(MoveTurret)
--Made by Ferrarico

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