mardi 31 mars 2015

Hinge joint 2D in Unity

I've been playing lately with a nice function in Unity, Hinge Joint 2D.

It's a feature which allows to joint multiple objects together. I can show how to use it in a simple way. I won't tell all the details as all this is quite easy to figure out and to do.

In the Unity editor, I put all my objects. Here, we're talking about two electric poles, and a stick (a simple rectangle of a few pixels) duplicated a lot of time.


On the first electric pole object, as you can see below, the usual components on a 2D object in Unity : Sprite Renderer to show the graphics, a BoxCollider 2D on pole's upper area (where the cable will be jointed) and a RigidBody 2D set as kinematic (so the object won't move).

And of course, a Hinge Joint 2D component, connected with the first cable object (just drag and drop the target object in the Connected Rigid Body box).


The stick-cable object (see below) is set as a child of the electric pole.


Same Components than before. The hinge joint is set to the next cable object (the same we're on, duplicated and moved to the left).


I repeat this for all cable objects needed, each one connected to the next one, except for the last one, connected to the second electric pole.

It's time to pray the pink unicorn and press play.


The result is good. This behavior (more or less bouncy) can be changed by tweaking the Rigidbody2D datas of every object.

In the game view :


Yummy!

Aucun commentaire:

Enregistrer un commentaire