Unity hook like the one seen on iOS Mikey Hooks game

As you can imagine, I liked a lot Mikey Hooks’ hook function, and I showed you working prototypes made with AS3+Box2D, AS3+Nape and Phaser+Box2D.

Now it’s time to see the same thing working with Unity. During the creation of this prototype you will learn, among other things, how to:

* create distance joints on the fly
* select a rigid body with the mouse
* draw lines on the stage

First, let’s have a look at the prefabs used in the prototype:

What we have in Hierarchy window is the Main Camera and a Game Engine empty object, where we are going to embed the main script. The game is also set to run at 640×480. If you have troubles to set up the camera, create game objects, change resolution or create and expand game prefabs, like the one we are going to see in a moment, there’s a detailed tutorial here.

Now, let’s have a look at Assets window:

* mainScene: the main scene, save it before you close the project

* MainScript: the main script, which will be added to Game Engine

* ground: the ground PNG image

* player: the player PNG image

* wall: the wall PNG image

* ground prefab: the prefab of the ground, containing the ground image, Rigidbody 2D component with Is Kinematic checked and Box Collider 2D component with default attributes

* wall prefab: the prefab of the wall, containing the wall image, Rigidbody 2D component with Is Kinematic checked and Box Collider 2D component with default attributes. Alto it’s tagged as “Wall”. Refer to this post if you aren’t familiar with Unity Tag feature.

* player prefab: the prefab of the player, containing the player image, Rigidbody 2D component and Box Collider 2D component with default attributes.

With this in mind, have a look at what we are going to build – remember it probably won’t work on Chrome:

You should know how to play: click and hold on a wall to fire the hook, release to destroy the hook.

And this is the commented source code of MainScript:

As you can see, it’s very easy to create a hook with Unity. As it was with other languages and frameworks.

Give me feedback and Download the source code.