x


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");

}

}

more ▼

asked Nov 28 '11 at 04:26 PM

Dreave gravatar image

Dreave
122 82 94 96

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 28 '11 at 10:33 PM

TheEmeralDreamer gravatar image

TheEmeralDreamer
171 47 64 70

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!

Nov 28 '11 at 10:50 PM jahroy

no problem!

Nov 29 '11 at 06:47 AM TheEmeralDreamer

Thanks for your help

Nov 30 '11 at 08:10 AM Dreave
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Nov 28 '11 at 10:27 PM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5056
x3768
x3718

asked: Nov 28 '11 at 04:26 PM

Seen: 795 times

Last Updated: Nov 30 '11 at 08:10 AM