Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Wesww · Jul 24, 2011 at 08:48 PM · animationinputcrossfadegetbutton

Crazy weird input script order glitch please help!

Unity will ONLY handle one of either my "walkLeft" or "walkRight" animations, depending on which one comes LAST in the order of the code:

So if I put this:

    if ( Input.GetButton("left") ) {
         dude.animation["walkLeft"].speed = 1;
         dude.animation.CrossFade("walkLeft");
         print("left");
     }
     if ( Input.GetButton("right") ) {
         dude.animation["walkRight"].speed = 1;
         dude.animation.CrossFade("walkRight");
         print("right");
     }

then "walkRight" will work when "right" is pressed, but "walkLeft" WILL NOT WORK when "left" is pressed.

Or, alternately, if I put this:

    if ( Input.GetButton("right") ) {
         dude.animation["walkRight"].speed = 1;
         dude.animation.CrossFade("walkRight");
         print("left");
     }
     if ( Input.GetButton("left") ) {
         dude.animation["walkLeft"].speed = 1;
         dude.animation.CrossFade("walkLeft");
         print("right");
     }

then "walkLeft" will work when "left" is pressed, but "walkRight" WILL NOT WORK when "right" is pressed.

print("right"); and print("left"); always work regardless of the order. What am I doing wrong here?!?! I'm banging my head against the wall trying to figure this out.

Full code:

 function FixedUpdate () {
 if( lockControls == false) {
     if (Input.GetButtonUp("Fire1")){
         idling = false;
         lockControls = true;
         cleanSound.PlayOneShot(cleanSound.clip);
         dudeControl.animation.Stop("guy_walk_sounds");
         dude.animation.CrossFade("clean");
         controlUnlock();
     }
     if ( Input.GetButton("left") ) {
         dude.animation["walkLeft"].speed = 1;
         dude.animation.CrossFade("walkLeft");
         print("left");
     }
     if ( Input.GetButton("right") ) {
         dude.animation["walkRight"].speed = 1;
         dude.animation.CrossFade("walkRight");
         print("right");
     }
     else{
         dude.animation["walkLeft"].speed = 0;
         dude.animation["walkRight"].speed = 0;
         dudeControl.animation.Stop("guy_walk_sounds");
         idling = true;
         Idle();
     }    
     
     var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
     var newPos = rigidbody.position + Vector3( (-keyboardX*walkDirec), 0, 0); 
     if (newPos.x < -1.66){
         newPos.x = -1.66;
     }
     else if (newPos.x > 1.52){
         newPos.x = 1.52;
     }
     rigidbody.MovePosition(newPos); // Move the object.
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SilverTabby · Jul 24, 2011 at 09:44 PM

This might not be the source of the problem but here's what I'm seeing:

 if (Input.GetButtonUp("Fire1")){
         //...
     }
     if ( Input.GetButton("left") ) {
         //...
     }
     if ( Input.GetButton("right") ) {
         //...
     }
     else{
         //...
     }   

Why do you only have 1 else for the entire block of ifs? If you are doing what I think you're trying to do, you need to this (one else per if):

     if (Input.GetButtonUp("Fire1")){
         //...
     }
     else if ( Input.GetButton("left") ) {
         //...
     }
     else if ( Input.GetButton("right") ) {
         //...
     }
     else{
         //...
     }   

I'm unsure if that will solve your problem, but it might.

Comment
Add comment · Show 4 · 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 Wesww · Jul 24, 2011 at 10:00 PM 0
Share

Yes, that is the problem. Thanks! I didn't know about "else if" statements.

You are a winner! This works now!

avatar image Wesww · Jul 24, 2011 at 10:04 PM 0
Share

That was seriously driving me crazy, I just had no idea what to even search for to try to learn how to fix this code.

avatar image Waz · Jul 24, 2011 at 10:24 PM 0
Share

There is no "else if" statement, its just a clear way of writing it. It is the same as:

     if (Input.GetButtonUp("Fire1")){
         //...
     } else
         if ( Input.GetButton("left") ) {
             //...
         } else
             if ( Input.GetButton("right") ) {
                 //...
             } else {
                 //...
             }   


SilverTabby's formatting is the better way to write it of course, but it is worth understanding exactly what you are doing.

avatar image SilverTabby · Jul 24, 2011 at 10:38 PM 0
Share

Here's a nice tutorial on if-else statements

http://download.oracle.com/javase/tutorial/java/nutsandbolts/if.html

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to animate two animation clips at the same time? 1 Answer

GetButton Problem 1 Answer

Animation problem in script 0 Answers

Problem with animations, How to blend them?? 0 Answers

Animation Issue 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