- Home /
Rotating a transform using it's child
I have a character that is made of a container GameObject named "Player", and it's child are the body, arms, legs, etc. I want it to turn and shoot at the direction the player clicks with the mouse, but I want it to turn using it's right arm as reference, not the whole Transform. This is how I'm doing it:
// 240, 160 is the center of the screen, the player is always centered var look : Vector3 = Vector3(mouse.position.x - 240, thisTransform.position.y, touchHandler.position.y - 160); thisTransform.forward = look.normalized;This is a script in the "Player" GameObject. If I try to use the right arm transform (rightArmTransform.forward = look.normalized), only the right arm would turn, not the whole body. How can I make the whole body rotate using the right arm?
Answer by stingman · May 11, 2012 at 06:13 AM
Access the parent of that object or the root of that object (depending on what's being parented to the arm:
transform.parent / transform.root. Adjust the transform of the parent accordingly. this should help you out:
http://unity3d.com/support/documentation/ScriptReference/Transform-parent http://unity3d.com/support/documentation/ScriptReference/Transform-root.html
Answer by Doft · May 13, 2012 at 07:39 PM
That's not really what I'm asking. Here's a screen to ilustrate what I want:
When you click on the screen, the player rotates so that the shot that comes from the arm will move in that direction. I'm having some trouble to find how much I should rotate. With the code above that I'm using, since the player's center is the capsule's center, the shot is a little offset. But if I rotate the right arm, it's also incorrect since it would only rotate the arm, not the parent. In the picture, if I click along the red lines the body would rotate 0/90/180/270 degrees, no matter where along the line I click.