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.
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?
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.
Wouldn’t the model break when they remove hinges tho?
“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.
Roblox is discontinuing hinges
What? Use HingeConstraints? Did you click the link?
Anyways, I’m still confused about this cframe stuff. How does it work?
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
I’m sorry about this, I’m still very confused. Do I put a hingeconstraint inside of the part I want to rotate?
It doesn’t matter. It’s the attachments that matter. Read the article linked
So something like if w pressed tween (rotation)?
No
Servo.TargetAngle = Servo.TargetAngle + SteerFloat * Speed
for example
Or also
Motor.AngularSpeed = SteerFloat * Speed
So, like a vehicle wheel but on its side?
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.
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.