- Home /
How to play multiple animations on the same game object.
I am creating a 3d endless runner game. Currently my game object contains two further child (Rider and the horse). The component (Animation)is attached to rider only and in the animator component play automatic is enabled. Now the problem is that its default animation is "horse_galloping" which is working fine. whenever i press "space" key jump animation plays once but the previous animation "horse_galloping" stops working, although the loop is enabled but still its not working after hitting space key. What i want is to keep playing animation "horse_galloping" after the "horse_jump" animation is played. Following is my code::
private Animation anim;
void start() { anim = mygamebobject.GetComponent ();
}
update ()
{
if (Input.GetKeyDown ("space"))
{
anim.CrossFade("horse_jump"); }
}
Animation component is attached to the gameobject (Rider). Parent GameObject is (HorseRider).
Have you read the documentation about the CrossFade function ?
http://docs.unity3d.com/ScriptReference/Animation.CrossFade.html
Fades the animation with name animation in over a period of time seconds and fades other animations out.
Answer by redeemer · May 28, 2015 at 12:38 PM
My first recommendation would be for you to change to Mecanim animation if possible. But if you intend to use Legacy I think this would help you : http://docs.unity3d.com/Manual/AnimationScripting.html
You can "play" with the weight of the animations. That means, an animation with higher weight would not be "cut" by an animation with a lower one. You just have to set the weight of your jump animation higher than the galloping animation and then, the galloping won't crossfade until the jump animation is over.
Hope I made my point, I've an example code but can't get to it right now. If you need it tell me and I'll post it when I get home.
Answer by PORUSH UNITY · May 29, 2015 at 08:00 AM
try to connect horse_galloping animation to horse_jump in animator window.so it come back to horse_galloping animation after the horse_jump animation complete.
I think he's using Lagacy (there is no animator), so he has to control the animation through code
Your answer
Follow this Question
Related Questions
Animator.CrossFade or Animator.CrossFadeInInFixedTime does not play animation after crossfading. 0 Answers
How can I smoothly transition my player camera from manual control into an animation? 1 Answer
Animation.Blend with Animator? 0 Answers
what is difference of animation blend and animation crossfade? 0 Answers
How to Loop a Animator Controller Component using wrapMode PingPong 1 Answer