1
\$\begingroup\$

I create a simple animation and an override controller for it with code.

//AnimationScript.cs
public Vector3 direction;
public float distance;

void Start()
{
    direction = direction.normalized;
    Vector3 posA = transform.position;
    Vector3 posB = posA + (direction * distance);

    AnimationClip clip = new AnimationClip();
    AnimationCurve curve = AnimationCurve.Linear(0.0f, posA.y, 1.0f, posB.y);
    clip.SetCurve("", typeof(Transform), "localPosition.y", curve);

    Animator anim = GetComponent<Animator>();
    AnimatorOverrideController animatorOverrideController = new AnimatorOverrideController(anim.runtimeAnimatorController);
    // "loop" is the name of clip not name of state
    animatorOverrideController["loop"] = clip;
    anim.runtimeAnimatorController = animatorOverrideController;
}

When I preview animation it works perfect, but when I export and play it, it does not start but camera motion works. What is the reason for that and how do I correct it?

Here are my object settings and controller:

object settings

base animation controller

\$\endgroup\$
1
\$\begingroup\$

The SetCurve API Documentation has this to say:

SetCurve will only work at runtime for legacy animation clips. For non-legacy AnimationClips it is an editor-only function.

I’m guessing that this is the problem, since you are working with an Animator.

\$\endgroup\$
  • \$\begingroup\$ You are right. Animation works with 'Animation' component. But when i run it i get exception: NullReferenceException: Object reference not set to an instance of an object UnityEditorInternal.AnimationWindowControl.GetAnimationModeDriver () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:474). \$\endgroup\$ – hal Dec 22 '18 at 12:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.