Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alex2670 · Jun 23, 2013 at 12:39 AM · animationmecanimunity 4

why can't i ask for more then 2, if and else statements in my script ? mecanim animation problem

(i'm using unity mecanim for the animation controls - version 4.1.5f1)

I have this problem with my script, the problem is that i can't ask for more than 2 if and else statements.

The only two animation that don't get enabled is "walk left" and "walk forward".

I have one script that controls my player movements and THIS script that controls my player animations on input.

Walk "left", "right" and "forward", all use the same animation, but there each have their own animation-tree attach to them. walk back have it's own animation.

(i know that there may be a better way to do this, but i need to know why this don't work)

i have tryed mixing the variables, to see if there is a problem with the connection to the other animation - trees, but that not it. (when i did this, "walk back" and "walk forward" don't get enabled)

according to my debug log, all the lines get executed when i click the keys, so that's not the problem.

here is the script, if there is someone that can help me, I would be really grateful.

 using UnityEngine;
 using System.Collections;
 
  
 public class characterContoller : MonoBehaviour {
     
     
     
     private Animator anim;                            // a reference to the animator on the character
 
     void Start ()
     {
         // initialising reference variables
         anim = GetComponent<Animator>();                                          
     }
         
     
     void Update ()
     {
         if(anim)
         {
             if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
             {
                 anim.SetFloat("Speed", 1f);
                 Debug.Log("walk forward");
             }
             else
             {
                 anim.SetFloat("Speed", 0f);
                 Debug.Log("ready for - walk forward");
             }
                     
             if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
             {
                 anim.SetFloat("Direction", -1f);
                 Debug.Log("walk left");
             }
             else
             {
                 anim.SetFloat("Direction", 0f);
                 Debug.Log("ready for - walk left");
             }
             
             if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
             {
                 anim.SetFloat("Direction", 1f);
                 Debug.Log("walk right");
             }
             else
             {
                 anim.SetFloat("Direction", 0f);
                 Debug.Log("ready for - walk right");
             }
             
             if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
             {
                 anim.SetFloat("Speed", -1f);
                  Debug.Log("walk back");
             }
             else
             {
                 anim.SetFloat("Speed", 0f);
                 Debug.Log("ready for - walk back");
             }
         }
 
     }
 }
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
0
Best Answer

Answer by Em3rgency · Jun 23, 2013 at 09:54 AM

You should use else if(condition) instead of just an unspecified else. Because, if I am reading this correctly, if no keys are pressed, all the if statements are false and all the else statements are true, so they all conflict at the same time.

Comment
Add comment · Show 2 · 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 alex2670 · Jun 23, 2013 at 06:03 PM 0
Share

thank you !!! i didn't see that my Float was overwritten by the else statements.

now i have create a float for each direction and it works.

thank you.

avatar image Em3rgency · Jun 23, 2013 at 06:07 PM 0
Share

Glad to be of help ;)

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

16 People are following this question.

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

Related Questions

Unity - Avatar problem? 0 Answers

Mecanim and 3Drt.com model problem 0 Answers

How do I force my character to a certain pose (T pose) in Unity 4 scene view? 3 Answers

Best Way to script Mecanim Animation CrossFade? 0 Answers

Blender animations to Unity problems 0 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