Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using a Switch/Case statement for movement

Discussion in 'Scripting' started by LLoydizle, Jan 30, 2015.

  1. LLoydizle

    LLoydizle

    Joined:
    Jan 15, 2014
    Posts:
    8
    Hello everybody!

    I am currently a newbie to programming and Unity itself (kinda learning both at the same time, as I started finding building console applications are getting extremely boring).

    My question is, In order to move a character in a 2D environment, I started out using if statements, and it worked perfectly fine. Although, instead of a bunch of if statements, i want to use switch/case.

    problem is: i get a ton of errors.

    The code is below:
    Code (CSharp):
    1.    
    2. public class Character : MonoBehaviour {
    3.  
    4.     float fMoveSpeed = 0.1f;
    5.     //float fJumpHeight = 5.0;
    6.  
    7.     void DoMovement()
    8.     {
    9.         switch (Input.GetKey) {
    10.         case KeyCode.D:
    11.             transform.Translate (new Vector3 (fMoveSpeed, 0, 0));
    12.             break;
    13.         case KeyCode.A:
    14.             transform.Translate (new Vector3 (-fMoveSpeed, 0, 0));
    15.             break;
    16.         }
    17.     }
    18.  

    Could anybody help?
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    why not use Input.GetAxis("Horizontal")
     
    MintBlast likes this.
  3. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    Errors are??
     
  4. LLoydizle

    LLoydizle

    Joined:
    Jan 15, 2014
    Posts:
    8
    Right now, I'm just on a learning phase with my programming. I'm actually learning with C++ but I could not find a another engine that's easy enough for me to script and learn in C++, so I'm giving Unity a try :). Cause of that, I'm not familiar with all the Unity functions yet lol.


    The errors are:
    Error CS0151: A value of an integral type expected (CS0151) (Assembly-CSharp)
    Error CS0029: Cannot implicitly convert type 'UnityEngine.KeyCode' to 'method group' (CS0029) (Assembly-CSharp)
    Error CS0029: Cannot implicitly convert type 'UnityEngine.KeyCode' to 'method group' (CS0029) (Assembly-CSharp)

    I am also calling my function in the Update, so I'm guessing its not that?
     
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    GetKey is a function, not a property. You can't use it with a switch (I mean, if you use it, you will have only two cases because it returns an boolean).
     
  6. LLoydizle

    LLoydizle

    Joined:
    Jan 15, 2014
    Posts:
    8
    Thanks for the reply :).

    Would using a switch/case not be a good idea for character movements?

    It works perfectly fine with if statements, I just wanna see if its possible with a switch/case?
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You shouldn't. With a if statement, you're checking if the returning value is TRUE or not. If we had an array of active keys, a switch could have been a good idea, but we don't.

    I don't really like switch ; I prefer use if statements and if there are too many "if / else if", maybe I did something wrong so I check for another solution.

    But you usually use switch when you want to enumerate something.. like an ENUM or an INTEGER :

    Code (CSharp):
    1.         public enum myEnum {
    2.             NONE,
    3.             SHOOT,
    4.             MOVE,
    5.             JUMP
    6.         }
    7.  
    8.         public void checkEnum() {
    9.             switch (playerState) {
    10.                 case myEnum.SHOOT:
    11.                     break;
    12.                 case myEnum.MOVE:
    13.                     break;
    14.                 case myEnum.JUMP:
    15.                     break;
    16.                 default:
    17.                     break;
    18.             }
    19.         }
     
  8. MarshET

    MarshET

    Joined:
    Feb 20, 2020
    Posts:
    1
    how do i make a w a s d movement controls with switch
     
    abtlb76 likes this.