How do I make a tank with a rotating turret?

I’m trying to make a tank game, I’ve already made the tank movable by WASD, now I want to make a turret that the player can rotate with two other buttons (undecided which ones), like F and G, though I’m not exactly sure how to do it. I can make the turret rotate, but I don’t know how to make the turret stay on the tank when the tank is moving. When I use weld constraints, when I try to rotate the turret, the whole tank rotates. Hard to explain so here’s a video:

2 Likes

You could connect the turret to the rest of the tank with a normal weld. Then you can just multiply the C0 of the weld with a CFrame created with the CFrame.Angles() function.

I’ve never worked with welds before so I might be missing something here, but when I use a normal weld, the whole tank rotates when I try to rotate only the turret.

Oh never mind that, I fixed it, but I’m kind of clueless on how to do the scripting part. (I’m quite the beginner at scripting and welds are new to me)

If you want to weld the turret to the rest of the tank in edit mode in studio, you can write something like this into your command bar

local hullPart = --the part in the tank's hull that you want to weld the turret to
loca turretPart = -- the part in the turret that you want to weld to the hull

local w = Instance.new("Weld")
w.Part0 = hullPart
w.Part1 = turretPart
w.C0 = hullPart.CFrame:ToObjectSpace(turretPart.CFrame) -- gets the relative CFrame of turretPart on hullPart's local axis and sets that as the C0.
w.C1 = CFrame.new()
w.Name = "TurretWeld"
w.Parent = hullPart

And, to rotate it in a script or local script you define the hullPart again and do something like this.

local weld = hullPart.TurretWeld
weld.C0 *= CFrame.Angles(0, --[[the angle value here]], 0) -- the angle must be a radian value. Degrees can be converted to radians with the math.rad function or *math.pi/180.