Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zachypin · Jun 13, 2011 at 06:41 PM · parentchildorientationboneorient

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image almo · Jun 13, 2011 at 06:49 PM 0
Share

Format your code. Select it, hit the 010101 button.

avatar image burnumd · Jun 13, 2011 at 07:29 PM 0
Share

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.

avatar image zachypin · Jun 13, 2011 at 07:48 PM 0
Share

you've got it

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 13, 2011 at 07:36 PM 0
Share

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.

avatar image burnumd · Jun 13, 2011 at 07:56 PM 0
Share

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.

avatar image zachypin · Jun 13, 2011 at 08:30 PM 0
Share

went into 3ds max and changed the hierarchy to make it mount at the right place. thanks!

avatar image
1

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges