I have been working on animations for my player character in my zombie shooter game.
When the player moves into the ‘run’ animation, the position of the hands change slightly, and then the gun is not aligned with the hands.
Here is a video:
http://videobam.com/oGxoJ
Could anyone suggest a fix?
The gun is a child of the player character.
The simplest way to resolve a problem like this is to understand how child/parent relationships work with objects.
You may or may not have come accross the the localPosition/localRotation/localScale properties of the Transform class. When you make an object a child to another object, either by dragging it over the object in the heirarchy or by setting it’s transform.parent to another transform (both methods do the same thing), the child object now uses the parent object as it’s origin. Although it may not be entirely obvious at first, object which appear to have no parent do infact have the “World” as a parent.
So what does all of this mean?
Well, the easiest way to understand all of this is to create a cube, and then set it’s position to 0,0,0. You will see that the cube is positioned to the center of the “World”, this is because the cube’s parent is the “World”. Now, create another block, position is at 10,0,10, create a sphere and child the sphere with the block you just created. You can see that the sphere is a child of the cube by clicking on the little > to the left of the cube and there you will see the sphere. Now, set the sphere’s position to 0,0,0. You will notice that the sphere is is in the center of the cube located at 10,0,10, therefore showing that child objects use the origin point of their parents as their own origin. Now if you move the cube currently located at 10,0,10, you will notice that the sphere remains it’s localPosition to the cube, the same thing occurs when you change the rotation and scale. (the local position is the position using it’s direct parent as the point of origin. The global position, or transform.position, is the position based on the “World” origin regardless of it’s child/parent relationship.
So, to answer your question, you will need to locate the bone of the hand of your character, and then set the gun as a child to that bone. This will cause any movement/rotation/scale that happens to the hand to pass down into the position/rotation of the gun.
Make a script that syncs the positions of hand and gun in LateUpdate. You have to choose the direction (hand controls gun or gun controls hand), probably the gun should be the one updating the hand.
(I wouldn’t mess with the animation hierarchy)