How do I script a tank seat that can rotate a gun and aim up and down?

I already have a system in place for firing missiles from a seat, but I don’t know how I would rotate or aim this turret. I am not very good at scripting so please explain in detail.

2 Likes

You may want to utilise TweenService for this. Tween the turrets CFrame as needed.

This article may help you:

As I said, I’m very inexperienced. How would I connect tween rotations to a keybind?

1 Like

Use something like the UserInputService or ContextActionService to listen to “keybinds”. You can also just use ThrottleFloat and SteerFloat in VehicleSeat properties. If you want it to aim with the mouse then you can use other things as well, but preferably the first 2 unless you want to aim in ThirdPerson mode.

You can use multiple things to rotate it like Constraints (hinges in this case), or just manually manipulate the CFrame by doing something like Y = Y + SteerFloat and then make a new CFrame using that angle.

1 Like

Wouldn’t the model break when they remove hinges tho?

1 Like

“Remove hinges”? What does this mean…? The hinges will be Motor or Servo constraints, attaching the tank, turret, and turret’s gun together in a way where they can rotate.

1 Like

Roblox is discontinuing hinges

1 Like

What? Use HingeConstraints? Did you click the link?

1 Like

Anyways, I’m still confused about this cframe stuff. How does it work?

1 Like

HingeConstraints are the simple solution. If you use CFrame directly, it will not replicate automatically (if done on the client) and what I meant was to use turretAttachCF * Angles(0,Y,0) and barrelAttachCF * Angles(X,0,0) or something

1 Like

I’m sorry about this, I’m still very confused. Do I put a hingeconstraint inside of the part I want to rotate?

1 Like

It doesn’t matter. It’s the attachments that matter. Read the article linked

1 Like

So something like if w pressed tween (rotation)?

1 Like

No
Servo.TargetAngle = Servo.TargetAngle + SteerFloat * Speed for example
Or also
Motor.AngularSpeed = SteerFloat * Speed

1 Like

So, like a vehicle wheel but on its side?

1 Like

I get everything else, but what is steer float?
Another thing:
Maybe the code could be like this.
(this is the event when “w” is pressed)
TankTurret.CFrame=TankTurret.CFrame*CFrame.Angles(0,math.rad(1),0)
end)
A something to keep in mind. I don’t remember my axes so it maybe different. E.g. CFrame.Angles(0,0,math.rad(1)) so just experiment.

1 Like

SteerFloat and ThrottleFloat are values from a vehicle seat, so if someone was in a turret seat, they can control it using vehicle controls.

Ohhhh. Thanks a lot. Now I understand. I’m going to quickly go make up a tank then!

This is actually way harder than I thought. I tried making a car with a vehicle seat but it doesn’t seem to work. Basically it’s like a wheel but a turret is attached with it. I’ll see if I can find a picture.

I suggest starting with learning how to rig the model appropriately without any scripts. You’re going to need a welded model (excluding the rotating bits–those will be held by HingeConstraints) with three parts: the base, the upper body, and the gun.

The base is just a platform for the upper half of the tank to be attached to. You’ll need one pivot point on the base to attach the upper body to. Place two attachments, one in the pivot point on the base, and the other on the upper half on the tank. Connect them appropriately using a HingeConstraint by setting the Attachment0 and Attachment1 properties appropriately. You can test the connection in studio by setting the HingeConstraint’s ActuatorType to Motor and giving it some fixed AngularVelocity.

For the gun/barrel, you attach it to a second pivot point that is on the upper body of the tank. It’ll need to rotate up and down, so make sure the attachments are oriented properly (you can check the axes of the attachments by selecting them in local space). You’ll use another HingeConstraint for this attachment, and you can test it in the same way you test the upper body.

I recommend familiarizing yourself with the structure of the vehicle you are trying to make before worrying about the coding as it will make it much easier to process what you need to do.

Here’s an example image of a turret I rigged in Studio a few minutes ago:

If you want to experiment with it, you can grab it using this link or using LoadAsset in an empty world:

It contains a quickly put together ControlScript, so there may be bugs in that, but this is more to show how the turret is rigged.

6 Likes