- Home /
Animation Scripting Help
I have an animation script for my walk and idle animation, I also have an animation for my shoot and reload (all scripts are at the bottom). But I have this problem that if I have both scripts attached to my gun the animations clash and dont work properly. I haven't done anything to the animation options on the gun inside the inspector window which I think I need to do, can anyone help me with this?
walk and idle script;
function Start(){
animation["walk"].speed = 1;
animation["idle"].speed = 0.5;
animation.wrapMode = WrapMode.Loop;
}
function Update () {
if (Input.GetAxis("Vertical") > 0.2)
animation.CrossFade ("walk");
else
animation.CrossFade ("idle");
}
shoot script;
function Start () {
animation["shoot"].speed = 2;
}
function Update () {
if(Input.GetButtonDown("Fire1")){
animation.Play("shoot");
}
}
reload script;
function Start () {
animation["reload"].speed = 1;
}
function Update () {
if(Input.GetButtonDown("r")){
animation.Play("reload");
}
}
Answer by TheEmeralDreamer · Nov 28, 2011 at 10:33 PM
Try setting each animation to an appropriate layer.
For instance if you have a ready animation for your character, you will want that animation to run all the time in a loop, however, you will also need attack animations and being struck by enemies,etc , to play.
So if you set the attack animations to play once and set them to a higher level, they will get priority, and once they are done playing, the ready animation will resume.
For the above code I believe you would need to set idle animation to layer 1. Then the walk animation to layer 2. The walk animation would need to be set to PingPong, whilst the idle animation would be set to loop.
Nice. Thanks for answering this and finally explaining to me what these mystical animation layers are.
I've seen no less than a half-dozen questions just like this one since I started playing with animations last week.
The documentation is very clear that playing an animation stops all other animations in the same layer. Now I know what that means!
Answer by jahroy · Nov 28, 2011 at 10:27 PM
I recommend reading the documentation for the functions you're using.
When you call Animation.Play or Animation.CrossFade, it effects all the animations that are currently playing (on the same layer).
I am very new to animations, but a quick scan of the documentation suggests that the animations triggered by your code will constantly interrupt each other.
The code that you've shared will cause the idle animation to play every frame that the up button is not being pressed. Otherwise, the walk animation will play.
If you want your shoot and reload animations to play, you probably have to stop your walk/idle animation (and vice versa).
Your answer
Follow this Question
Related Questions
Trying to simply play this animation... Help please. 1 Answer
Reacting to Mouse animation... 2 Answers
Loop animation on button hold 2 Answers
How could it be done "Cooldown" for a shield ? 2 Answers
play animation sequence 3 Answers