- Home /
Orient parent to child location
I have a sword in my game that I am attaching to my character based on tags.
var target : Transform;
function Start ()
{
target = GameObject.FindWithTag("RHand").transform;
transform.parent = target.transform;
transform.localPosition = Vector3(0, 0, 0);
}
My sword is the parent object, there are 2 children, blade and hilt. I've attached this code to the hilt so that the character is holding onto the sword by the right place. How can I get the blade to orient itself to the right location based on where the hilt is? I'll eventually change this from a Start function to an Update function so when I walk around the sword stays in my hand, currently just need some way to get it all attached.
Thanks!
Also, if you post the same question to Unity Forums, please provide a link (like this one: http://forum.unity3d.com/threads/92961-Orient-parent-to-child-location ) so people aren't spending time answering a question that's already been answered somewhere else.
Answer by Bunny83 · Jun 13, 2011 at 07:27 PM
It seems you don't understand how parent-child relationship works. A child only have one parent. If the hilt is a child of your sword and you set the parent of the hilt to something else, it's no longer a child of the sword. It's not related anymore to the sword. If you want the hilt-position to be the attachpoint of the sword make it the parent of your sword. You can do this:
function Start ()
{
var target = GameObject.FindWithTag("RHand").transform;
var sword = transform.parent;
transform.parent = target.transform;
sword.parent = transform;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
}
-RHand
-Sword
-blade
-hilt
After the start function:
-RHand
-hilt
-Sword
-blade
It would be much easier to position the childs inside the sword the right way, so that the hilt will be at the same position as the sword. Then you can attach your script to the sword so the sword will be attached to the hand.
That is definitely the easiest option, to modify the model so it mounts more naturally. I was operating under the impression that the OP was using a purchased model or for some reason didn't want the sword to have its pivot be the hilt.
went into 3ds max and changed the hierarchy to make it mount at the right place. thanks!
Answer by burnumd · Jun 13, 2011 at 07:33 PM
You want to offset the sword's new local position by the hilt's offset relative to the sword. So what you want is the negative offset of the hilt. After the sword (not the hilt) is parented to the hand, set the sword's local position to be the opposite of the hilt's local position. So:
transform.localPosition = -Vector3.Scale (hilt.localPosition, transform.localScale;
This code would go on the sword (not the hilt, as you have it now) and you have to scale it by the sword's scaling just in case it's not (1,1,1).
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Check if there is a child of object, and if so, get tag? 2 Answers
bones axis orientation strange when importing from blender (axis don't match) 2 Answers
Determining What an Object is parented to 1 Answer
How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene? 1 Answer