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

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.

I will try tommorow, and I will tell if it worked. Because I need to go rightnow.

Alright, I can provide you more information and resources then

1 Like

I don’t really want to use BodyGyro or any of that, because I need to rig the model. I am trying to make a script that rigs everything automatically. I would rather to do it with CFrame. My entire tank is based on CFrame except for pushing it forward and backwards.

1 Like

It doesn’t work, it just throws turret into mouse position.

You can subtract the mouse CFrame and the turret CFrame to get aim. Issue is it would move the entire turret. Im not experienced enough with CFrames to know how to only make it rotate on the Y Axis only. You could start off by using CFrame:ToWorldSpace()so it always rotates on the Y-Axis even at a slant, and maybe you could find a way to rotate it on the Y Axis exclusivly. I‘ll try some methods, I‘ll update if I find anything relevant!

1 Like
TurretController.C0 = CFrame.new(TurretController.C0.p, 
    Vector3.new(Mouse.Hit.p.X, TurretController.C0.LookVector.Y, Mouse.Hit.p.Z)
)

I did this, but it doesn’t rotate always, I am not sure why.

Bumping this post because I still don’t know why it doesn’t always turn to my mouse position, I think camera is effecting it but idk why.

How about CylindricalConstraint?

CylindricalConstraint.TargetAngle = Mouse.Hit.LookVector.Y

I am not sure on how to get number from Mouse Position to do that.

Sorry for the bump, but now I made a resource to help you out:

There are a lot of things you have to do like CFrame:Inversing() the part 0 in order to obtain the correct rotation relative to the world which this module does for you so go check it out.

1 Like