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.
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.
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
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)
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.