- Home /
Animation interference problem
I have wrote this script that plays my animations to what keys and buttons are being held down, I have recently added in the walk left and right animations. They do not play when A or D is pressed where as if I put it into a different script and take off the script that has the shooting animations on it works fine. What's going wrong and how can I fix this?
function Start () {
animation["walk_left"].speed = 1.5;
animation["walk_right"].speed = 1.5;
animation["walk_left"].wrapMode = WrapMode.Loop;
animation["walk_right"].wrapMode = WrapMode.Loop;
animation["idle"].speed = 0.5;
animation["walk"].speed = 2.5;
animation["reload"].speed = 1.5;
animation["shoot"].speed = 6;
animation["idle"].wrapMode = WrapMode.Loop;
animation["walk"].wrapMode = WrapMode.Loop;
animation["shoot"].wrapMode = WrapMode.Once;
animation["reload"].wrapMode = WrapMode.Once;
animation["shoot"].layer = 1;
animation["reload"].layer = 2;
animation.Stop();
}
var fullClip : int = 8 ;
var bulletsLeft : int = 8 ;
function Update () {
if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("walk");
else
animation.CrossFade("idle");
if(Input.GetKeyDown("a")){
animation.CrossFade("walk_left");
}
if(Input.GetKeyDown("d")){
animation.CrossFade("walk_right");
}
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot");
}
}
if(Input.GetButtonDown("r")){
animation.Stop();
animation.CrossFade("reload");
bulletsLeft = fullClip ;
}
}
Ok one thing: can you format your code? You just highlight it and push the button with the "0" and "1" on it.
Secondly - it looks a bit like you are doing a cross fade to idle all of the time in the update when vertical
If I take else idle out of the script, it works fine is there any other way to add the idle animation if nothing else is playing or something?
It seems strange that you have an "else" statement in the middle of a long series of "if" statements. Put the "else" statement at the end, and have all of the "if" statements after the first "if" statement be "else if" statements. Not only will the script be clearer, but it will work better because the "else" statement is where it needs to be.
Answer by whydoidoit · May 26, 2012 at 03:46 PM
So (per the comments above) @Dreave would like the idle to be playing when nothing else is.
To do that you can put the animations on different layers - the higher the number the more important the priority. I can see you have already got that for reload and shoot. Make each of those +1 and make your walk animations = 1 then leave idle on 0. Start playing it in Start and when nothing else is playing it will be shown.
Firstly you shouldn't need those animation.Stop() calls as the layers should override the other animations so drop them.
Now your problem then is that you want to turn on and off the walk animations.
You could .enable all of your walk animations in Start and set the weight to 0. E.g.
animation["walk_left"].enabled = true;
animation["walk_left"].weight = 0;
The instead of
if(Input.GetKeyDown("a")){
animation.CrossFade("walk_left");
}
You could do
if(Input.GetKeyDown("a")){
animation.Blend("walk_left",1);
} else {
animation.Blend("walk_left", 0);
}
And the same for walk right.
I have only done this to my walk_left animation to see if it works, unfortunately im still having the problem here's my code, is it all correct?
function Start () {
animation["walk_left"].speed = 1.5;
animation["walk_right"].speed = 1.5;
animation["walk_left"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["walk_right"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["idle"].speed = 0.5;
animation["walk"].speed = 2.5;
animation["reload"].speed = 1.5;
animation["shoot"].speed = 4;
animation["idle"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["walk"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["shoot"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation["reload"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation["idle"].layer = 0;
animation["walk"].layer = 1;
animation["walk_left"].layer = 1;
animation["shoot"].layer = 2;
animation["reload"].layer = 3;
animation["walk_left"].enabled = true;
animation["walk_left"].weight = 0;
animation.Stop();
}
var fullClip : int = 8 ;
var bulletsLeft : int = 8 ;
function Update () {
if($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("walk");
else
animation.CrossFade("idle");
if(Input.Get$$anonymous$$eyDown("a")){
animation.Blend("walk_left",1);
} else {
animation.Blend("walk_left", 0);
}
if(Input.Get$$anonymous$$eyDown("d")){
animation.Play("walk_right");
}
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Play("shoot");
}
}
if(Input.GetButtonDown("r")){
animation.CrossFade("reload");
bulletsLeft = fullClip ;
}
}
Nope not quite!
Remove the
else
animation.CrossFade("idle")
From Update();
$$anonymous$$ove the animation.Stop() to the beginning of Start()
Play the idle animation in Start().
Try this:
function Start () {
animation.Stop();
animation["walk_left"].speed = 1.5;
animation["walk_right"].speed = 1.5;
animation["walk_left"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["walk_right"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["idle"].speed = 0.5;
animation["walk"].speed = 2.5;
animation["reload"].speed = 1.5;
animation["shoot"].speed = 4;
animation["idle"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["walk"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
animation["shoot"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation["reload"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation["idle"].layer = 0;
animation["walk"].layer = 1;
animation["walk_left"].layer = 1;
animation["walk_right"].layer = 1;
animation["shoot"].layer = 2;
animation["reload"].layer = 3;
animation["walk_left"].enabled = true;
animation["walk_left"].weight = 0;
animation["walk_right"].enabled = true;
animation["walk_right"].weight = 0;
animation["walk"].enabled = true;
animation["walk"].weight = 0;
animation["idle"].enabled = true;
animation["idle"].weight = 1;
}
var fullClip : int = 8 ;
var bulletsLeft : int = 8 ;
function Update () {
if($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1) {
animation.Blend("walk", 1);
} else {
animation.Blend("walk", 0);
}
if(Input.Get$$anonymous$$eyDown("a")){
animation.Blend("walk_left",1);
} else {
animation.Blend("walk_left", 0);
}
if(Input.Get$$anonymous$$eyDown("d")){
animation.Blend("walk_right", 1);
} else {
animation.Blend("walk_right", 0);
}
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Play("shoot");
}
}
if(Input.GetButtonDown("r")){
animation.Play("reload");
bulletsLeft = fullClip ;
}
Im still having this problem, I have taken the shoot and reload functions out and put them in a different script so its a bit easier to read but my left, right and idle animations still dont play, what's going wrong?
You are very welcome. It took us a while but we got there :)
Your answer
Follow this Question
Related Questions
Animation not working Properly (Blender to Unity) 1 Answer
adding animation dynamically by script 0 Answers
Animation Script Help 0 Answers
How to keep an animation attached to gameobject 0 Answers
Animation scripting problems 1 Answer