• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Dreave · May 24, 2012 at 08:52 PM · animation

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 ;
     }
 }
Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 24, 2012 at 10:15 PM 1
Share

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

avatar image Dreave · May 25, 2012 at 03:40 PM 0
Share

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?

avatar image You! · May 26, 2012 at 03:22 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreave · May 26, 2012 at 04:34 PM 0
Share

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"].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 = 4;

animation["idle"].wrapMode = WrapMode.Loop;

animation["walk"].wrapMode = WrapMode.Loop;

animation["shoot"].wrapMode = WrapMode.Once;

animation["reload"].wrapMode = WrapMode.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(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)

animation.CrossFade("walk");

else

animation.CrossFade("idle");

if(Input.GetKeyDown("a")){

 animation.Blend("walk_left",1);

} else {

 animation.Blend("walk_left", 0);

}

if(Input.GetKeyDown("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 ;

}

}

avatar image whydoidoit · May 26, 2012 at 05:36 PM 0
Share

Nope not quite!

Remove the

 else
      animation.CrossFade("idle")

From Update();

Move the animation.Stop() to the beginning of Start()

Play the idle animation in Start().

avatar image whydoidoit · May 26, 2012 at 05:43 PM 0
Share

Try this:

 function Start () {

     animation.Stop();
     
     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 = 4;
     animation["idle"].wrapMode = WrapMode.Loop;
     animation["walk"].wrapMode = WrapMode.Loop;
     animation["shoot"].wrapMode = WrapMode.Once;
     animation["reload"].wrapMode = WrapMode.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(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1) {
         animation.Blend("walk", 1);
     } else {
         animation.Blend("walk", 0);
     }
 
 
 
     if(Input.GetKeyDown("a")){
         animation.Blend("walk_left",1);
     } else {
         animation.Blend("walk_left", 0);
     }
 
     if(Input.GetKeyDown("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 ;

 }
avatar image Dreave · May 26, 2012 at 09:36 PM 0
Share

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?

avatar image whydoidoit · May 26, 2012 at 10:39 PM 1
Share

You are very welcome. It took us a while but we got there :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Help 4 Answers

Reloading Help 2 Answers

Animation Scripting Help 2 Answers

Stop animation after played 30 times? 0 Answers

Animation doesn't play when calling it with a script!! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges