Posts

Moving hing joints via code

The other day I wanted to build a type of projectile launcher that would allow a user to move the barrel from left to right, with restricted angles of about 45 degree angles to each side.  I was surprise to find out that the hinge joint itself didn't really offer a way to do this, but instead I had to find a combination to settings that would allow me to rotate the barrel.  I started by downloading a free turret model from the Unity Asset Store. Once I had the model in the scene, I found the base I needed control over and added a Hinge Joint to it.  I modified the Anchor and the Axis, set Use Limits to true and under Min set it to -45 and Max to 45. The min and max will give me + and minus 45 degrees of movement from whatever starting position the object is in. In code all I needed was to rotate the model and the hinge joint would take care of not rotating it more than the limits set in the join. This is the TurretScript public class TurretScript : MonoBeh
Recent posts