How would I rig + set up this turret model?

I have this turret model, a Flak 88, that I bought from an asset pack last year.

It’s pretty solid, yet, I am having some issues trying to figure out how to rig it so that it pivots in the following ways:
A) it spins around, which I have been able to do with HingeConstraint here
B) its angle will change, via going up and down, kind of like here, but with some limitations

I was thinking of commissioning this out, but I want to be able to figure out how to use constraints confidently so I can do this myself for future updates/games.

I have a similar cannon model that I use in one of my games, but it is a free model that I did not code myself.

It functions similarly to how I want the Flak 88 to move around, as seen here. That being said, this cannon model uses welds to move around, and not constraints - I am pretty certain that for the Flak 88, using constraints is the way to go.

How would I rig the Flak 88 up? What constrains would I use? I am pretty certain that I would need to use AlignPosition and AlignOrientation. And how would I code it so that, when a player is in a vehicle seat, the gun goes up/down, or turns based on the vehicle seat’s steer/throttle? Here’s the code that the cannon from my existing game works:

angle = 0

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	if script.Parent.Steer == 1 then
		script.Parent.Swiv:play()
		script.Parent.Parent.WER.Value.C0 = script.Parent.Parent.WER.Value.C0 * CFrame.fromEulerAnglesXYZ(0,0.01,0)
	elseif script.Parent.Steer == -1 then
		script.Parent.Swiv:play()
		script.Parent.Parent.WER.Value.C0 = script.Parent.Parent.WER.Value.C0 * CFrame.fromEulerAnglesXYZ(0,-0.01,0)
	elseif script.Parent.Steer == 0 and script.Parent.Throttle == 0 then
		script.Parent.Swiv:pause()
	end
	if script.Parent.Throttle == 1 and angle < 20 then
		script.Parent.Swiv:play()
		angle = angle + 1
		script.Parent.Parent.WER2.Value.C0 = script.Parent.Parent.WER2.Value.C0 * CFrame.fromEulerAnglesXYZ(-0.01,0,0)
	elseif script.Parent.Throttle == -1 and angle > -15 then
		script.Parent.Swiv:play()
		angle = angle - 1
		script.Parent.Parent.WER2.Value.C0 = script.Parent.Parent.WER2.Value.C0 * CFrame.fromEulerAnglesXYZ(0.01,0,0)
	end
end)
1 Like

EDIT: Here’s a better GIF of how I would like the Flak 88 to move up and down:
https://gyazo.com/d7ab4e451ad43d1e7320ae3c7d359447

EDIT 2: I think, for getting the spinning to move, I will make use of the HingeConstraint properties, like AngularVelocity, to just have that change based on if the vehicle seat steer is -1 or 1, kind of like this: https://gyazo.com/d08f8473609789d6ee9caef44670b3dd

I am still not sure how to approach the pivoting the gun up and down though…

1 Like

You can use another hinge constraint for pivoting up and down.

For Hingeconstraint if you want mouse controls you can use this function to calculate the servo angle.

2 Likes

Thanks for the resource!

I was intending to keep this system entirely based on the VehicleSeat’s properties, to help keep this system very cross-platform compatible, rather than using a mouse, where it might not work as well on say, mobile.

I ended up using a HingeConstraint for the up and down movement. However, it doesn’t actually move the gun, even though the angle is indeed increasing up and down, as seen in this GIF

I’m not sure if I screwed up the constraints (set up for them is here and here), or it’s my code, which is here:

local seat = script.Parent
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	if seat.Steer == 1 then
		script.Parent.Parent.Parent.SM_Prop_German_Flak_01_Pivot.HingeConstraint.AngularVelocity = .25
	elseif seat.Steer == -1 then
		script.Parent.Parent.Parent.SM_Prop_German_Flak_01_Pivot.HingeConstraint.AngularVelocity = -.25	
	else
		script.Parent.Parent.Parent.SM_Prop_German_Flak_01_Pivot.HingeConstraint.AngularVelocity = 0
	end
	if seat.Throttle == 1 then
		script.Parent.Parent.Parent.gun.SM_Prop_German_Flak_01_Body.Left.TargetAngle += 1
		script.Parent.Parent.Parent.gun.SM_Prop_German_Flak_01_Body.Right.TargetAngle += 1
	elseif seat.Throttle == -1 then
		script.Parent.Parent.Parent.gun.SM_Prop_German_Flak_01_Body.Left.TargetAngle -= 1
		script.Parent.Parent.Parent.gun.SM_Prop_German_Flak_01_Body.Right.TargetAngle -= 1	
	end
end)
1 Like

Ended up fixing the issue.

Here it is in motion!
https://gyazo.com/36b2fbad283086fc2c7ab1b894475784

BTW, would this work for controlling both the horizontal and vertical rotation? Right now, I just have a vehicle seat that uses throttle and steer to move up/down and right/left, but for some turrets, I will have to use a mouse controller.

1 Like

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