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 Answer 1

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\$
1
  • \$\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\$ Commented Dec 22, 2018 at 12:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.