- Home /
How to give a direction to a hook?
Hi,
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.
Answer by robertbu · Feb 02, 2014 at 10:05 PM
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.
Hi,
Thanks for the quick answer. I want the hook to go down of the screen but with an angle and the angle need to be the angle of the hook (it always swings and it should go down with the angle of the moment which player clicked on the space button).
Check this video as an example: Video
"and it should go down with the angle of the moment"
And that is what the Transform.Translate() does that I outlined in my answer. Alternately you can do something like:
transform.position += transform.up * speed * direction * Time.deltaTime;
Note I'm assu$$anonymous$$g the hook is authored so that the bottom of the hook is looking at negative 'y' when the rotation is (0,0,0). If you've constructed the hook so that the hook portion looks at positive 'z' ins$$anonymous$$d, change the 'transform.up' to 'transform.forward', and you would use the 'z' parameter in the Translate().
Your answer
Follow this Question
Related Questions
Wheel Friction Curve in 2d 0 Answers
2D Physics: How to have a character "grab" onto wall with hand 0 Answers
HingeJoint2D.GetReactionForce not working 1 Answer
Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers
Relative Joint2d: Is there a way limit force to only X or Y axis? 0 Answers