- Home /
Scripted animation not working
Hello everyone.
I made a run-time script that is creating a .anim animation.
Here what I got:
// new clip
var clip = new AnimationClip();
// new curves
var posXcurve = new AnimationCurve();
var posYcurve = new AnimationCurve();
var posZcurve = new AnimationCurve();
// gameobject's original local position (all animations are done locally)
Vector3 originalPos = target.transform.localPosition;
// set start keyframes
posXcurve.AddKey(0, originalPos.x);
posYcurve.AddKey(0, originalPos.y);
posZcurve.AddKey(0, originalPos.z);
// set end keyframes
posXcurve.AddKey(1, 5);
posYcurve.AddKey(1, 5);
posZcurve.AddKey(1, 5);
// apply curves to clip
clip.SetCurve(target.name, typeof(Transform), "localPosition.x", posXcurve);
clip.SetCurve(target.name, typeof(Transform), "localPosition.y", posYcurve);
clip.SetCurve(target.name, typeof(Transform), "localPosition.z", posZcurve);
clip.wrapMode = WrapMode.Once;
clip.name = "position";
target.animation.playAutomatically = false;
target.animation.AddClip(clip, clip.name);
target.animation.clip = clip;
target.animation.Play();
if (target.animation.isPlaying)
{
Debug.Log("playing");
}
When hitting play, I can see that the "target" was given a clip named "position" and the console shows "playing" as expected.
But... the target is not moving!
If anyone have any ideas to why the gameobject is not playing the animation, that will be great.
Thanks!
Are you playing the right animation? Where did you create the animation? Did you split the animations into the appropriate frames? $$anonymous$$ore information about the animation itself will be very helpful.
I'm defiantly playing the right animation (there is only one :) ).
This is the script that is creating the animation, the animation does not exist before the script runs.
As you can see in the script, I'm creating 2 frames, a start frame at time 0 and an end frame at time 1.
Well, if you don't $$anonymous$$d posting the entire script, I'd be happy to test it out and troubleshoot. The code you actually posted shows no methods like Update(), Start(), etc...
Just put the entire thing under start ( I made it under a method that is called from start, just to make it cleaner).
And change "target" to gameObject and put it on a gameobject with an animation component.
Answer by dorpeleg · Jul 29, 2013 at 07:54 AM
OMG, so stupid.
After reading SetCurve again, I noticed this line:
If relativePath is empty it refers to the game object the animation clip is attached to.
So I changed this:
// apply curves to clip
clip.SetCurve(target.name, typeof(Transform), "localPosition.x", posXcurve);
clip.SetCurve(target.name, typeof(Transform), "localPosition.y", posYcurve);
clip.SetCurve(target.name, typeof(Transform), "localPosition.z", posZcurve);
to this:
// apply curves to clip
clip.SetCurve("", typeof(Transform), "localPosition.x", posXcurve);
clip.SetCurve("", typeof(Transform), "localPosition.y", posYcurve);
clip.SetCurve("", typeof(Transform), "localPosition.z", posZcurve);
And it works!
I feel dumb :P
for posting resolution. You're learning, so that says you're not dumb lol. Happy Developing!