Jump to content

Howto -- Animation.AddClip and Playing the Clip


Recommended Posts

In the following, I'm attempting to create a Clip and play it. I'm successfully instantiating the animation object (anim). I verified that "openClip" is not null. However, when I execute "anim.Play("openScreen")" for the clip, the clip does not play. I verified the return value, which is TRUE. That is, Unity thinks it played the clip. The model has a single baked animation "Scene", which is from frames 1 through 89. Scene will eventually have other clips.


private Animation anim;
private AnimationClip openClip;

public override void OnStart(PartModule.StartState state)
{
anim = part.FindModelAnimators("Scene")[0];
openClip = new AnimationClip();
anim.AddClip(openClip, "openScreen", 1, 89, false);
anim.Play("openScreen");
}

I could create the clips in the Unity Editor. However, I want to attach AnimationEvents to the clips. Specifically, I want to know when each clip has finished playing.

So:

1) what is incorrect in my method of creating and playing clips in the script?

2) can I attach an AnimationEvent to a clip I define in the Unity Editor versus doing it in the compiled script?

Bonus question: How can I play a clip backwards? That is, is this legal: anim.AddClip(closeClip, "closeScreen", 89, 1, false);

note that the Start frame is greater than the End frame

Link to comment
Share on other sites

I'm not sure about the animationclip parts. But to get the animation to play you should just need to set the speed, and maybe the time, and use Play.


anim["scene"].speed = 1f;
anim["scene"].normalizedTime = 0f;
anim.Play("scene");

To play it backwards you would just set the speed to -1f, and the time to 1f, you can also use regular time (in seconds) instead of normalized time.

Link to comment
Share on other sites

Thanks for the info about playing it backwards. I do want to be able to play clips, i.e., not the entire Scene. Also NEED to add AnimationEvents which can be added to only AnimationClips. Also, I can play the entire Scene without any setup. I just use:

anim.Play();

Here is the code block:


private AnimationEvent openEvt;

openEvt = new AnimationEvent();
openEvt.functionName = "ModifyScreen";
openEvt.time = 89;
openEvt.stringParameter = "open";

openClip = new AnimationClip();
// openClip.AddEvent(openEvt);
anim.AddClip(openClip, "openScreen", 1, 89);
anim.Play("openScreen");
Debug.Log("length=" + openClip.length);

When I've got the clip playing, then I'll uncomment the AddEvent. And, as I mention above, the clip is three seconds. However, openClip.length shows it as only one second.

Edited by Apollo13
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...