Tank Turret Rotation Issue

Hello,

So I’ve started making my own tanks system and ran into a big issue.

My tank turret rotates the wrong way. Which would be shown in the picture below.

It’s supposed to rotate around the TurretHolder part, I’m sure you guys know how a tank turret works.

So, what would the solution to that be? Here’s the local script too.

local RunService = game:GetService("RunService")

local Workspace = game:GetService("Workspace")

local Players = game:GetService("Players")

local Motor = workspace:WaitForChild("M1 Abrams"):WaitForChild("Turret"):WaitForChild("TurretHolder"):FindFirstChildWhichIsA("Motor6D")

local Mouse = Players.LocalPlayer:GetMouse()

local DegPerSec = 5

local radPerSec = math.rad(DegPerSec)

local lookVec = CFrame.new(Motor.Parent.Position, Mouse.Hit.Position).LookVector

local currentAngle = math.atan2(lookVec.X, lookVec.Z)

RunService.Heartbeat:Connect(function(delta)
	local targetDir = Motor.Parent.Position-Mouse.Hit.Position
	local targetAngle = math.atan2(targetDir.X, targetDir.Z)
	local difference = targetAngle-currentAngle
	local angleToAdd
	if math.abs(difference) > math.pi then
		angleToAdd = -math.sign(difference)*(math.abs(difference)-math.pi)
	else angleToAdd = difference
	end
	angleToAdd = math.clamp(angleToAdd, -radPerSec*delta, radPerSec*delta)
	Motor.C0 *= CFrame.Angles(0, angleToAdd, 0)
	currentAngle += angleToAdd
end)

All answers are appreciated.

1 Like

I think it has an issue with the axis, have u tried changing it

1 Like

this is an issue with how your tank motor6d’s are rigged, the orientations are off

2 Likes

That’s because your currentAngle is set to the angle between (0,0,1) and your lookVec.
What you want is the angle between (0,0,1) and your targetDir.

1 Like

I did try to change the axis. Yet it changed from going side to side to up and down.

1 Like

Hmm Okay. I’ll try to apply that and will get back to you.

1 Like

nevermind, i’ve found the solution for it. All I needed to do was just weld to head to an another part with the correct Motor6D Orientations and FrontFace then just make a motor6D and connect it to the TurretWelder part.

1 Like

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