In this tutorial I will be teaching you the basic of how to use hinge constraints with the example of a simple turret. I will be focusing on building in studio, though if it's requested I can make a scripting tutorial as well.
Step one: Creating the hinge
First go to the model tab (Above) and enable the buttons marked (Below) Once you've done that, click the drop down triagle under "Create"
That will give you a list of constraints to choose from, go ahead and click "Hinge"
Right now studio is waiting for you to click on two parts to create the attachments and the hinge, I will be clicking in the two places marked below. To make life easier on yourself, I would recommend seperating your parts far enough to see both faces you'll be clicking.
And there you go! Your hinge is added! But we're not done yet.
Step 2: Positioning the hinge
Now that the hinge is created, you might notice a red line between the two attachments, that's because they aren't alligned.
I'm going to go ahead and move my parts together and...
Whoops! It looks like the attachments didn't line up!
If attachments don't line up like this when you start the game, it can have an impact on the performance. Don't sweat though! Attachments have a some very useful properties, WorldPosition, WorldOrientation, and WorldCFrame (Not pictured)
You can copy and paste these properties to align the other attachment's world position and world orientation with the original.
Warning! This will most likely cause your attacment to become off centered, and might cause some imbalance in the physics! If you're using this for wheels I would advize that you align the part rather then the attachment. (I will go into more detail about when and why you should do this in another tutorial)
Fortunately for me, the part was simple .1 stud to the side, so I didn't need to do this, however with more complicated models, it may becaome something you need to do more often.
Step 3: Configuring the Servo
Let's see what properties we're working with, I'll go by catagory. (Minus Data)
Apperance: This modifies what the hinge looks like in game, however most of the time visibility is set to false, so I won't get into this much.
Behavior: The most noticable difference here is the "Enabled" property, which when set to false will unlink the hinge. Nothing too special.
Hinge: This is where things get interesting we have "ActuatorType" and "LimitsEnabled". ActuatorType is basically what kind of forces the hinge will apply to the two parts. LimitsEnabled will allow you to set limits to how far the hinge can twist.
Derived: This has one read only property; "CurrentAngle". This is used in scripting to see what angle the hinge is at from it's normal angle.
Attachments: Rather simple, it holds what attachments the hinge is linked to.
With that out of the way, go ahead and click the drop down for Actuator type, and select "Servo".
Look at that! A whole new catagory appeared!
This is where the fun begins
If you have two parts similar to what I have in this tutorial, time for a little demonstration!
Set the property "AngularSpeed" to 1 and the property "MaxTorque" to 20.
On the top bar of studio, click "Test", and where there's the button that says "Play", click the drop down, and hit "Run", this should launch your game. If your model falls to the ground, you might want to stop the session, anchor one of the parts, and run it again.
Now that your game is running, find the hinge instance (It should be parented to one of the two parts) and start changing the "TargetAngle" value to something between -180 and 180. Assuming everything went well, it's moving! Congradulations! However there's one more thing you should know.
Click on the part that's turning, change it's marerial to be Metal, and then start changing the TargetAngle value again.
Uh oh! It's not moving nearly as fast now is it? That's because changing the material to Metal made it heavier. This is where Constraints become a little tricky, you'll have to fine tune your constraints to be able to support just enough weight that they can easily move the parts you've linked, but not so much that it causes it to misbehave.
I wont be getting into how to script servos in this tutorial, since this is designed for setting up the constraints. However, if you would like to jump into scripting right away, TargetAngle is what you should be changing in your code.
Step Four: LimitsEnabled
Using limits for your hinge, or any constraint for that matter, allows you to limit how much it can move, or in this case, twist, along it's free axis. But before we get into how they work, let's create the next part of our little model.
As you can see, I added two parts to the sides, one of which is moved off to the side. I did this so I could more easily add the next hinge constraint.
"But wait!" you exclaim, "There's two parts, why aren't they both moved aside so I can add a hinge to both?"
Good question! It's actually good practice to use as few constraints as you can get away with, and since in this case both of the parts will be rotating the same way, there's only a need for one hinge. Another example: In real life a door might have 3 hinges, but in roblox you only need 1 hinge constraint to get the same effect.
Following the steps above, I connected the parts with another hinge constraint, and set the AngularSpeed to 1, and the ServoMaxTorque to 500. Now we're going to click "LimitsEnabled", and set it to true.
Look at that! Another catagory with 3 more values!
LowerAngle and UpperAngle are pretty intuitive, they are what you use to set the edges of the rotation limit. What might be a little confusing is that middle property, Restitution.
Restitution is a value between 0 and 1 that determins how "bouncy" the limits are when the hinge tries to turn past them. At low speeds, (AngularSpeed between 1 and 5), changing this value has little to no effect on how the hinge behaves, so I will be leaving it at 0.
The keen eyed among you might have also noticed that there's also something new on the hinge itself...
This is simply a visualization of the limits that we added, to allow you to more easilly adjust the values to what you need. After some experimenting, I have decided to set the LowerAngle to -45 and the UpperLimit to 20.
We're almost ready to test again! You might be wondering how the second part will move with the first one, and the way things are now, it will just fall to the ground, not connected to anything. But there's another constraint that fixes this problem very easily! It's the WeldConstraint .
All we need to do is click the constraint drop down, find the one that says "Weld", click it, then click our two parts! If you click the "Show Welds" button above the "Constraint Details" button, it'll show a little green bar that connects the two parts. That's our weld!
Guess what! We're ready to test again, hooray!
Follow the same steps again to run, then start messing with the hinge constraints to see how it behaves. You'll notice that the two parts wont rotate past the limit angle that you set, exactly what we wanted!
This tutorial showed you how to use hinge constraints as servos for a small turret, but they can be used for so much more! For example, you can use them on a door or small hatch, or maybe for a laptop's screen. You might be thinking of using this for the wheels on a car, however I would only recommend using it for turning, as there are better constraints to use for constant spinning. (Hint: Try setting the ActuatorType to Motor!) Servos shine the most when used to set and hold the angle between parts, and aren't meant for constant spinning.
Good luck and have fun!
I recognize that this tutorial is specifically for hinge constraints and studio, and doesn't cover a lot about scripting them. I also realize that there may be a lot more about constraints that should be covered, and if the want is there I can make more tutorials on the rest of the constraints. So please leave a comment letting me know which constraints you would like me to cover next!
Med Written By NotAPorgu
See NotAPorgu's profile on Roblox