How to rotate tank turret with Weld.C0 CFrame [still need help]

I am trying to make tank system, I got stuck on turret rotation. I’ve tried this:

local hullPart = workspace["E-100"].Hull
local turretPart = workspace["E-100"].Turret

local w = Instance.new("Weld")
w.Part0 = hullPart
w.Part1 = turretPart
w.C0 = hullPart.CFrame:ToObjectSpace(turretPart.CFrame)
w.C1 = CFrame.new()
w.Name = "TurretWeld"
w.Parent = hullPart

local mouse = game.Players.LocalPlayer:GetMouse()

local weld = hullPart.TurretWeld
while wait() do
	local n = math.rad(mouse.Hit.YVector.Y * 10)
	print(n)
	weld.C0 *= CFrame.Angles(0, n, 0)
end

I am not quiet sure on how to do it. It can be hinge constraint but I prefer weld method to rotate the turret. I just need to get angle to make the turret point at.

Yes I should use RunService instead of loop

Try:

local LookVector = mouse.Hit.LookVector

weld.C0 = CFrame.new(weld.C0.Position, weld.C0.Position + Vector3.new(0, LookVector.Y, 0))

This makes the C0 to a cframe that’s origin is the current c0 position, then makes that is based off the c0’s current position + a vector representing the y component of the look vector.

might work but im not sure

2 Likes


It doesn’t work, oof.
The turret just looks up and does nothing.

Are you trying to only rotate about the y axis, or all 3 axes

lookVector instead of LookVector?

They’re the same property. lookVector however is deprecated in favor of LookVector.

This logic applies to :connect and :Connect. They’re the same function, but :connect is deprecated in favor of :Connect

1 Like

I think its only Y rotation. Something like World Of Tanks.

Do you want only the Y rotation though, or do you want all 3 axes.

I only want to rotate the turret in like circle, only Y rotation I believe.
EachPaltryBaboon-size_restricted
Something like World Of Tanks Blitz does have.

Rotating turret around 360 degrees. Gun itself up and down, thats it.

Raycasting isn’t needed to get an angle.

I couldn’t figure out how to do it with welds so I tried a direct CFraming approach.

Are you trying to do this?

Good job, yes it can be. What about the turret?

? The black bar represents the barrel of the turret while the blue hinge reprsents the axle the turret turns about.

Oh, ok. I didn’t know that. It looks like it works well from looking at your .gif.

This is what I’m doing

RunService.Heartbeat:Connect(function()
	local MouseVector = Mouse.Hit.LookVector
	local YAngle = math.asin(MouseVector.Y)
	
	Barrel.CFrame = (Hinge.CFrame * CFrame.Angles(YAngle, 0, 0)) * CFrame.new(0, 0, - Barrel.Size.Z / 2)
end)

Barrel is the tank barrel.
Hinge represents where the end of the barrel sits.

2 Likes

Yeah, you are helping me a lot here. I hope you will find the solution, I am trying too.

I had a similar thing I wanted to do, but it was a ship turret. What I would recommend is having a BodyGyro inside that turret, and have it be on a HingeContraint. The Hingeconstraint wont do anything, it would just keep the turret attached to the vehicle while allowing sideways movement.

Here’s a line taken from my ship turret script:
script.Parent.BodyGyro.CFrame = CFrame.new(script.Parent.Position, mouse.p)

here, the BodyGyro made the gun move smoothly to where the mouse was. Turret Speed can be adjusted by BodyGyro.P Which is why I like this method. Though I just joined this conversation, so someone else could have suggested something better

2 Likes

This is what I used for my laser gun script. Same concept.
script.Parent.Equipped:Connect(function(mouse)
local char = game.Players.LocalPlayer.Character
mouse.Button1Down:Connect(function()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.IgnoreWater = true
local raycast = workspace:Raycast(script.Parent.Handle.Position, (mouse.Hit.Position - char.PrimaryPart.Position).Unit * 50, rayParams)
local laser = Instance.new(“Part”, workspace)
laser.Size = Vector3.new(1, 1, (mouse.Hit.Position - char.PrimaryPart.Position).magnitude)
local forwardVector = (mouse.Hit.Position - char.PrimaryPart.Position).Unit
local upVector = Vector3.new(0, 1, 0)
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)

	laser.CFrame = CFrame.fromMatrix(char.PrimaryPart.Position, rightVector, upVector2) * CFrame.new(0,0, -(mouse.Hit.Position - char.PrimaryPart.Position).magnitude / 2)
end)

end)

1 Like

Thank you very much, I figured on how to make fully working tank, the up and down gun works. The only thing I need is CFrame.angles based rotation for tank turret, it doesn’t need involve welds.

have you tried the BodyGyro thingy I did?
It should make the turret move by the Y axis by where your mouse is pointing at.