- Home /
How do I rotate the hook?
I have a character which throws a hook. I want the hook to rotate in the direction I'm adding velocity/specific target. I'v tried using this:
transform.TransformDirection(hookDirection);
hookDirection is the direction of target or the velocity I'm throwing. I'm calling it when I'm pressing the input. It's not working as I expected. What am I missing here? Any help?
Edit: I'm looking for immediate rotation. It's a 3d game and the z-axis negative in forward direction.
'Transform.TransformDirection()' takes a direction in local coordinates and convert that direction into world coordinates. It has no impact on the rotation of an object. In addition, the way based on the way you describe it, 'hookDirection' is already in world coordinates. As for what you are trying to do, whoever answers will need some information:
An immediate rotation (as when you launch) or a rotation over time?
If a 3D app, did you model your hook so that the 'forward' side of the hook is facing positive 'z' when the rotation is (0,0,0)?
If a 2D app, did you model your hook so that the right side is the sprite is forward?
Answer by robertbu · Feb 27, 2014 at 07:25 PM
You can use:
transform.rotation = Quaternion.LookRotation(-hookDirection);
If you are Instantiating the object, you can use the the Quaternion call in the Instantiate() call. The negative sign is because you say the 'forward' is facing negative 'z'.
The z angle is somehow not correct, x and y angles are good. I checked it by pausing the scene. When I adjusted the z angle, it looks fine. What about if I have no target? should the hookDirection can be velocity direction?
Is the 'z' angle off by 180 degrees or by something random? Unity wants the 'forward' of an object to be facing positive 'z' when the rotation is (0,0,0), and 'up' to be facing positive 'y'. So if you have the option, I'd change your model in your modeling program to fit this description. It may or may not solve your 'z' rotation problem, but it will make it far easier for anyone on this list to provide you workable solutions.
Note there is an optional second parameter to LookRotation() that defines what is considered 'up' and is used (when the rotation allows) as the axis of rotation. You may need to set this second parameter to get the rotation you want. Without an understanding of how the 'z' rotation is wrong, I cannot tell how to set this second parameter.
I'll look into that. Thanks! What about velocity direction? If I have no direction, Can I use velocity direction in LookRotation?
The direction of your velocity will work fine, though you will have to negate it if your forward is still facing negative 'z'.
Your answer
Follow this Question
Related Questions
Rotate angle 0 Answers
Flip over an object (smooth transition) 3 Answers
Rotating Camera around player object 0 Answers
Getting the rotation of an object does not return correct rotation 1 Answer
Object instantiate, Projectile problems 2 Answers