Search Unity

Adding Root Motion Animation Via Script

Discussion in 'Animation' started by qtoompuu, Jan 3, 2020.

  1. qtoompuu

    qtoompuu

    Joined:
    May 8, 2017
    Posts:
    6
    I am working on a project that requires me to animate a humanoid rig via script, I have worked out how to get the muscles animated, but ran into a problem with the animation not accounting for Root Motion. To try and fix this, I took the appropriate Rotational values and stored the respective XYZW values in their own Keyframe[] Arrays and did the same with the Positional XYZ values. Then in the exact same fashion as I previously added the rigs Muscle Keyframe[] Arrays to the animation clip, I tried to add the Root T and Root Q attributes as such;

    Code (CSharp):
    1. curve = new AnimationCurve(rotationX);
    2.             clip.SetCurve("", typeof(Animator), "Root Q.x", curve);
    3.             curve = new AnimationCurve(rotationY);
    4.             clip.SetCurve("", typeof(Animator), "Root Q.y", curve);
    5.             curve = new AnimationCurve(rotationZ);
    6.             clip.SetCurve("", typeof(Animator), "Root Q.z", curve);
    7.             curve = new AnimationCurve(rotationW);
    8.             clip.SetCurve("", typeof(Animator), "Root Q.w", curve);
    9.             curve = new AnimationCurve(positionX);
    10.             clip.SetCurve("", typeof(Animator), "Root T.x", curve);
    11.             curve = new AnimationCurve(positionY);
    12.             clip.SetCurve("", typeof(Animator), "Root T.y", curve);
    13.             curve = new AnimationCurve(positionZ);
    14.             clip.SetCurve("", typeof(Animator), "Root T.z", curve);
    Please note that the Vars rotationX etc are all of type Keyframe[]. This worked with all the muscles, but when doing this with Root Q and Root T, the frames showed as yellow in the Animator Window. The part that I don't understand, is that I was then Able to click Add Property > Animator > Root Q and Root T and add them. I could also then Copy Paste The Keyframes from the yellow Root Q and Root T into the ones I added manually, and the animation played perfectly

    In the above Image, the Root Q and T that are yellow and say 'Missing' were added by the script, the ones that are normal, I added by hand after the fact. Once Copy and Pasted over the animation looked exactly as I wanted. also when I expand the Attributs in the Animator window, the sub values can be seen to be the same

    Does anyone know how to fix this?
     
  2. qtoompuu

    qtoompuu

    Joined:
    May 8, 2017
    Posts:
    6
    Sorry I realised the image did not work for some reason here it is