Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Orient parent to child location

Discussion in 'Scripting' started by zachypin, Jun 13, 2011.

  1. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    I have a sword in my game that I am attaching to my character based on tags.

    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Start ()
    5. {
    6.  
    7.     target = GameObject.FindWithTag("RHand").transform;
    8.     transform.parent = target.transform;
    9.    
    10.     transform.localPosition = Vector3(0, 0, 0);
    11. }  
    12.  
    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!
     
  2. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    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 is parented to the hand, set the sword's local position to be the opposite of the hilt's local position. So:
    Code (csharp):
    1. transform.localPosition = -Vector3.Scale (hilt.localPosition, transform.localScale;
    You have to scale it by the sword's scaling just in case it's not (1,1,1).
     
  3. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Also, cross-posting the same question to Unity Answers is fine, but if you do so, please provide a link (like this one) so people aren't spending time answering a question that's already been answered somewhere else.
     
  4. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    My sword in the hierarchy is setup as "energy sword" as the parent. And underneath that is "box001", "box002", "cylinder001".
    The cylinder is the hilt and the boxes are two pieces to the sword.

    I have the following code on the cylinder:

    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Start ()
    5. {
    6.  
    7.     target = GameObject.FindWithTag("RHand").transform;
    8.     transform.parent = target.transform;
    9.    
    10.     transform.localPosition = Vector3(0, 0, 0);
    11. }
    12.  
    Then on the two boxes I have the following:

    Code (csharp):
    1.  
    2. var hilt;
    3.  
    4. function Update ()
    5. {
    6.         hilt = GameObject.FindWithTag("Hilt").transform;
    7.     transform.localPosition = -Vector3.Scale (hilt.localPosition, transform.localScale);
    8. }
    9.  
    The sword pieces do move but they don't line up correctly so I'll have to keep playing around with it.
     
  5. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Just put the script on the "energy sword" object (the top-level Transform for the sword). Then set up a public variable for the "hilt" (as you did with the "target") and set "cylinder001" to be that hilt variable in the Inspector.
     
  6. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    I just noticed this bit:
    Why would you do that? The whole point of the Transform parent-child relationship is that you simply need to set the object A (the highest level part of a single object) as a child of some other object B and suddenly everything that is a child of object A *and* object A itself will move around with object B wherever it goes without any further work on your part. I'd suggest re-reading the manual on Transforms and doing some experiments yourself on how the system works before you get into more complicated matters like weapon mounting and the like.
     
  7. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    easier fix was going into 3ds max and changing the hierarchy to make the hilt the parent object.
    Thanks for the response!
     
  8. zachypin

    zachypin

    Joined:
    Jun 7, 2011
    Posts:
    33
    Because before I wasn't attaching the sword via the parent. I was going to be updating the rest of the sword constantly. Now that the hierarchy is corrected I don't need the update function.