- Home /
Adding another animation to this script
I currently have idle, walk and sword attack animations running fine for my character using the script from unity docs. The problem I am having is when I try add another animation. The newer animation added will stop all the rest from working, for instance if I add "WalkLeft" that will be the only one to play.
The input settings are done so would this small piece of code just need to be added to the script or have a made a mistake:
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1)
animation.CrossFade("WalkLeft");
The code I have used is the following one:
function Start () {
// Set all animations to loop
animation.wrapMode = WrapMode.Loop;
// except shooting
animation["SwordAttack1"].wrapMode = WrapMode.Once;
// Put idle and walk into lower layers (The default layer is always 0)
// This will do two things
// - Since shoot and idle/walk are in different layers they will not affect
// each other's playback when calling CrossFade.
// - Since shoot is in a higher layer, the animation will replace idle/walk
// animations when faded in.
animation["SwordAttack1"].layer = 1;
// Stop animations that are already playing
//(In case user forgot to disable play automatically)
animation.Stop();
}
function Update () {
// Based on the key that is pressed,
// play the walk animation or the idle animation
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Walk");
else
animation.CrossFade("Idle");
// Shoot
if (Input.GetButtonDown ("Fire1"))
animation.CrossFade("SwordAttack1");
}
Any help would be appreciated.
Can you explain more? you add the code and then what happens? do the WalkLeft play and nothing else plays after it? is it something else?
Answer by dorpeleg · Jan 17, 2013 at 06:16 PM
Try writing it like this:
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){
animation.CrossFade("Walk");
}else if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1){
animation.CrossFade("WalkLeft");
}else
animation.CrossFade("Idle");
See if it works for you.
That works perfectly!!!!!!!!!!!!
I played around for a while (newbie), in the end it was as simple as that.
Thanks for your help
Answer by masterp · Jan 17, 2013 at 05:24 PM
Basically you got it,if I add my walkleft code no other animations will play other than that one
Please put your replay as a comment next time. Do the other animations not work from the start? or only after you run the WalkLeft once?
If i use the larger piece of code i posted above, my walk, idle and sword attack work. $$anonymous$$y problem is that if another animation is added to this code only the newer animation will play, all others stop.