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 Kammerman02 · Apr 18, 2015 at 11:53 PM · movementspritecharactercontrolswalking

When walking, my character will not change sprites.

As the title explains, I have a game object with three sprite animations in it:an idle, walking left, and walking right animation. When play the game, I can move left and right fine, but the sprites are not functioning correctly. The walking animations play at the same time even though I am not pressing any keys and the idle animation does not play at all. Here is my script:

 static var speed : int = 1;
      public var CLEFT : GameObject;
      public var CRIGHT : GameObject;
      public var CIDLE : GameObject;
      
      function Update () {
              if (Input.GetKey (KeyCode.LeftArrow)) transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
              {
                  CLEFT.gameObject.active = true;
                  CIDLE.gameObject.active = false;
              }
              if (Input.GetKey (KeyCode.RightArrow)) transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
              {
                  CRIGHT.gameObject.active = true;
                  CIDLE.gameObject.active = false;
              }
      
     }

Any feedback would be greatly appreciated!

Comment
Add comment · 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 MayhemMike · Apr 19, 2015 at 06:17 PM 0
Share

Did you try to add an else statement that starts the idle animation when no button gets pressed?

avatar image Kammerman02 · Apr 20, 2015 at 02:30 PM 0
Share

I tried adding an else statement but now it is pulling multiple errors:

 #pragma strict
 
  static var speed : int = 1;
  public var walking_left : GameObject;
  public var walking_right : GameObject;
  
 function Update()
 {
     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
     {
         walking_left.gameObject.SetActive (true);
     }
     
     else{
         walking_left.gameObject.SetActive (false);
     }
  
      if (Input.Gey$$anonymous$$ey($$anonymous$$eyCode.RightArrow))transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
      {
          walking_right.gameObject.SetActive (true);
      }
      
      else{
          walking_right.gameObject.SetActive (false);
      }
 }
avatar image MayhemMike · Apr 20, 2015 at 03:39 PM 0
Share

It is not the most elegant way but try this:

 function Update()
  {
      if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
      
      transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
      
          walking_left.gameObject.SetActive (true);
          //code to turn off the idle animation goes here
      }
      
   
       else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
       transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
       
           walking_right.gameObject.SetActive (true);
           //code to turn off the idle animation goes here
           
       }
       
       else{
            walking_left.gameObject.SetActive (false);
            walking_right.gameObject.SetActive (false);
            
           //code to activate your idle animation goes here
       }
  }

  

Your brakets were out of place and there was a spelling error with "Input.Gey$$anonymous$$ey". I also added else if statements. You only want one of the 3 states (w-left;w-right and idle) to trigger so they should be connected.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by maccabbe · Apr 20, 2015 at 04:44 PM

There are many problems with your original code. The most worrysome is that you are trying to use both the line of text and block of code following your if statement. However the following code

 if(condition) // line of code;
 {
    // block of code;
 }

is the same as

 if(condition){
     // line of code
 }
 if(true){
     // block of code
 }

You should instead combine the original line of code and block of code into a new block of code

 if(condition){
    // line of code
    // block of code
 }

In addition you should use if/elseif/else statments so you can set the idle animation. Also since CLEFT, CRIGHT, and CIDLE, are gameObjects this is redundant to get their gameObject. You should also use SetActive instead of active if you want to change a gameObject's activity. Finally, it is easier to read Vector3.left and Vector3.right than Vector3(1, 0, 0) and Vector3(-1, 0, 0). Applying all these changes results in the following

 static var speed : int = 1;
 public var CLEFT : GameObject;
 public var CRIGHT : GameObject;
 public var CIDLE : GameObject;
       
 function Update () {
     if (Input.GetKey (KeyCode.LeftArrow)) 
     {
         transform.Translate (Vector3.left * Time.deltaTime*speed);
         CRIGHT.SetActive(true);
         CLEFT.SetActive(false);
         CIDLE.SetActive(false);
     }
     else if (Input.GetKey (KeyCode.RightArrow)) 
     {
         transform.Translate (Vector3.right * Time.deltaTime*speed);
         CRIGHT.SetActive(false);
         CLEFT.SetActive(true);
         CIDLE.SetActive(false);
     }   
     else{    
         CRIGHT.SetActive(false);
         CLEFT.SetActive(false);
         CIDLE.SetActive(true);
     }
 }

http://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements

Comment
Add comment · Show 1 · 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 Kammerman02 · Apr 21, 2015 at 01:10 PM 0
Share

Okay I understand my problem. Thank you everyone for resolving my issue, I will make sure to handle my if/else statements better in the future.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character doesen't collide 3 Answers

character turning where he looks 0 Answers

I want to play an Audio Sound for a character while running. 0 Answers

I Watched The Unity New Ball Rolling Game Tut, How I Can Make Jump,I Watched The New Unity Ball Rolling Tutorial, How I Can Make Jump? 0 Answers

Character Animation Question (Bear Force One) 1 Answer


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