How to give a direction to a hook?


I’m very new to Unity and have a question for my school project.

At the top of my Unity 2D scene, there is a hook which needs to have a natural swing left to right. I tried to handle this by adding a hingeJoint to the top of the hook and then applying a velocity to rigidbody2D.


My problem is sending the hook to the down when played clicked on the space button on the keyboard. Because, the hook is always swinging, when the space button has clicked, it should go to the correct direction depending on the looking direction of the hook and then should come back after touching to the end of the camera view.

I’ve spent hours on internet in order to find a method to determine correct vector to go down and then go up but all of the examples I could found were about calculating the vector between two known points. In this scenario, I don’t have the direction point but only the angle of the transform.

Any help will be appreciated.

Another point: At the moment, when player has clicked on the space button, I am disabling the hingeJoint and converting the body to a kinematic object in order to be able to change the location of the hook to the down. I also appreciate if you have another idea about this part.

How you do this will depend on how you want to move the hook. The world direction will be ‘transform.up’ for up direction and ‘-transform.up’ for down direction. These can be used for adding force for example. Or you can use Transform.Translate() which, but default, takes local coordinates. So you can move it up by:

transform.Translate(0.0, speed * Time.deltaTime * direction, 0.0);

…where ‘direction’ is 1 for up and -1 for down.