Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Screenhog · Oct 17, 2012 at 06:54 PM · quaternionparentbone

How do I parent an object to a bone?

(I realize this question comes up frequently, however, the many answers didn't seem to address my specific issue.)

I have a gun. I want to parent it to the hand of a character. Here is my simple code for doing so.

 var weapon: GameObject;
 weapon = Instantiate (gun, worm.leftWrist.position, worm.leftWrist.rotation);
 weapon.transform.parent = worm.leftWrist;
 Debug.Log (worm.leftWrist.rotation+" "+worm.leftWrist.localRotation);

The first two lines, by themselves, work fine. The gun lands in exactly the right place. However, the gun isn't parented to the bone.

The third line parents the gun to the bone. This resets the gun's rotation to a default rotation. In an attempt to figure out why the gun's rotation changes on parenting, I wrote the fourth line, the Debug.Log. The result I get from Debug.Log is that both rotations are Vector4 (0.0, 0.0, 0.0, 1.0). However, when I actually go to the bone at the point of parenting, the rotation of the bone in the Inspector is X = 270, Y = 90, Z = 0. I don't know Quaternions very well, but I'm pretty sure the Quaternion for that is not 0, 0, 0, 1.

So, how do I get the actual rotation value of a bone so that I can rotate the weapon properly after parenting the transform?

Comment
Add comment · Show 7
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 Muuskii · Oct 17, 2012 at 07:13 PM 0
Share

So. . . when the wrist moves, the gun doesn't?

avatar image Screenhog · Oct 17, 2012 at 08:29 PM 0
Share

No, when I parent the gun, it does move with the wrist. But the rotation, which is originally right, changes to an incorrect rotation. I'm trying to fix that.

avatar image Muuskii · Oct 17, 2012 at 08:36 PM 0
Share

Have you tried not altering the rotation at all and just letting the parent properties do their job?

I just tested having two cubes: scale (0.5, 0.5, 2) the second one parented to the first with rotation (0, 90, 0) and position (1.5, 0, 0.65)

When I rotate the parent "cube" the other follows appropriately. NO scripts involved. Do you need similar functionality?

avatar image Screenhog · Oct 17, 2012 at 08:43 PM 0
Share

If it was one normal object parented to another, it would work fine. The fact that I'm parenting to a bone that's part of a skeleton seems to be causing the issue.

avatar image $$anonymous$$ · Oct 17, 2012 at 09:39 PM 0
Share

oh sorry, didnt read carefully, I am sorry i couldnt help you out

Show more comments

2 Replies

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

Answer by whydoidoit · Oct 17, 2012 at 10:14 PM

The localRotation will be Quaternion.identity because the object has the same rotation as the parent (you set it that way). .rotation will be the rotation of the parent. In other words as the gun has the same rotation as the parent then its localRotation will be Quaternion.identity.

If you want it to have some other rotation, parent it first and then set the rotation or set the rotation to be some other value in the Instantiate.

Comment
Add comment · Show 1 · 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 Screenhog · Oct 17, 2012 at 10:33 PM 0
Share

Yeah, I think what's been happening is that I've been setting it to the bone's rotation before animation affects it. I'll check and see... if that's the case, I'll mark this answer accepted.

avatar image
0

Answer by $$anonymous$$ · Oct 17, 2012 at 08:37 PM

You have to write

weapon.transform.parent = worm.leftWrist.transform;

instead of just the Object.

Comment
Add comment · Show 2 · 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 Screenhog · Oct 17, 2012 at 08:41 PM 0
Share

It's not obvious from the code snippet I gave, but worm.leftWrist is a Transform, not a GameObject.

avatar image Muuskii · Oct 17, 2012 at 08:51 PM 0
Share

It kinda is clear because you say: Instantiate (gun, worm.leftWrist.position . . .

however the following line: Vector3 vec = gameObject.position;

Will throw the error:

Type `UnityEngine.GameObject' does not contain a definition for `position'

Because GameObject. . .does not have a position, Transform does.

Anyway. . . just a comment.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert quaternion to vector3 with specific multiplication order? 5 Answers

Bone rotation in LateUpdate goes back to normal every frame 2 Answers

Orient parent to child location 2 Answers

Weapon Parenting...v.2.0 1 Answer

Angle of bones to read the same as the inspector. 2 Answers


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