- Home /
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?
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.
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?
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.
oh sorry, didnt read carefully, I am sorry i couldnt help you out
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.
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.
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.
It's not obvious from the code snippet I gave, but worm.leftWrist is a Transform, not a GameObject.
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
![](https://koobas.hobune.stream/wayback/20220613082510im_/https://answers.unity.com/themes/thub/images/avi.jpg)