- Home /
How to get and use animation clips of a model via script?
Hi!
I need to play the animations of an object on the object itself, and I need to do that at runtime. But how can I get the animation clips of the object so I can assign them to the AnimationController?
Thanks in advance and have a nice day!
Answer by Yuvii · Oct 09, 2017 at 02:19 PM
Hey,
Not really sure about what you're asking but i'll give a try. When you have your fbx file with your animation(s) imported in unity, then click on it and in the Inspector>Animations you have a timeline wich represents the animation(s) timeline in your fbx file (of course).
Let's assume that the complete timeline defines only one animation, like the walk loop. (then you probably should rename that animation "walk" (set it above the timeline)). If you go to the animator window (Window>Animator) you should see your walk animation in it (in orange if it's the default animation, which means it will play the animation on start of animator) ot grey (not default). You should have at least two animations if you want to be able to switch from one to an other.
Then in Animator Window>Parameters you can set your own parameters that you will be able to modify in script.
For example, let's say i have two animations : Idle and Walk. I have one parameter which is a boolean 'isMoving'. So i make a transition in the animator window from Idle to walk and set the condition to 'isWalking' > true and a transition from Walk to Idle with the opposite condition.
Now in the code, it's basically something like that
Animator anim;
public h;
public v;
void Start(){
anim = GetComponent<Animator>();
}
void Update(){
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
if(h != 0 || v != 0){
anim.SetBool("isWalking", true);
}
else{
anim.SetBool("isWalking", false);
}
}
Hey thanks for the answer!
I'm sure this will be very useful for a lot of people, but I think I didn't explain well, because my problem was to extrapolate and assign to an Animation Controller, the Animation Clips inside a model.
However I solved it by using Asset Bundles and the command LoadAllAssets(). The Asset Bundle contained my .fbx model and all its component split into single and accessible components (this was automatically made by Unity at the moment of creating the bundle).
As I said, thanks anyway for the answer and have a nice day!