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

Question GetAxis to GetKey Conversion Help?

Discussion in 'Scripting' started by JWdell, Oct 17, 2022.

  1. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    I have this bit of code in a game I have where the way everything is, works perfectly fine and has for a long time now. I recently decided to make some changes that would add custom keys to allow a player to change the keys but I'm stuck figuring out how to rewrite this bit of code as I don't understand the issue nor know how to resolve it. Any help in the right direction is appreciated.

    What I have below is the variable and the line of code that gives the issue. As you can see, the working code calls the listing in the Input Manager and the commented out code was my attempt to change it to the custom key stuff I added. The custom key stuff works for everything else so it isn't the issue, it is just this specific line of code I'm stuck on since I don't understand how you would convert from using GetAxis to GetKey with the custom key listing. When I attempt this which is clearly wrong, I get an error "Argument 1: cannot convert from 'bool' to 'float'. Is this something that would need a complete rewrite or am I just not using the proper syntax in the way this should be rewritten? This is the part of the code that has the issue. Just need some guidance in the right direction as I've never had to do this before until now.

    Code (CSharp):
    1. public Vector3 velocity = Vector3.zero; // shorthand
    2.  
    3. void Update()
    4.     {
    5.         var aniPlay = GetComponent<aniSprite_C>();
    6.         CharacterController controller = GetComponent<CharacterController>();
    7.  
    8.         if (controller.isGrounded ) // player is grounded
    9.         {
    10.             jumpEnable = false; // reset jumps
    11.            
    12.             //velocity = new Vector3 ( Input.GetKey ( KeysManager.KM.moveleft ), 0f, 0f); // new error
    13.             velocity = new Vector3 ( Input.GetAxis ( "Horizontal" ), 0, 0 ); // old working - we are ground, so recalculate direction directly from axis
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Pull the code apart so there are completely-distinct steps:

    - read all the input, putting the data into temp variables

    - process all the input variables appropriately

    - clear all the temp input variables and start over

    That way you can trivially change the input stage.
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Well it's pretty clear that you're trying to put a bool into a slot that takes only floats, just do some simple if statements.
    GetKey 
    returns a bool, so you need to transform that into a float.

    Code (CSharp):
    1. float value;
    2. if(Input.GetKey(KeysManager.KM.moveleft))
    3.     value = 1;
    4. else
    5.     value = 0;
    6. velocity = new Vector3(value, 0f, 0f);
    No shame in adding extra lines of code.
     
  4. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    OK cool, that seems to have done the trick. I had to make some adjustments to support left and right movement since the old code shared the Horizontal left/right from the Input Manager.

    Code (CSharp):
    1. float value;
    2.             if(Input.GetKey(KeysManager.KM.moveleft))
    3.                 value = -1;
    4.             else
    5.             if(Input.GetKey(KeysManager.KM.moveright))
    6.                 value = 1;
    7.             else
    8.                 value = 0;
    9.             velocity = new Vector3(value, 0f, 0f);
    Only one issue I came across is when you're in the air and not grounded which is written differently. I tried using this same method for it but it doesn't seem to work.

    Code (CSharp):
    1.  
    2. if (!controller.isGrounded ) // player is in the air
    3.         {
    4.             //velocity.x = Input.GetKey ( KeysManager.KM.moveleft ); // new error
    5.             velocity.x = Input.GetAxis ( "Horizontal" ); // old working - set horizontal speed from input
    6.            
    7.             if ( moveDirection == 0 ) // left
    8.             {
    9.                 if ( jumpEnable )
    10.                 {
    11.                     velocity.x *= moveSpeedJump;
    12.                 }
    13.                 else // this else is working and fixes ledge falloff velocity
    14.                 {
    15.                     velocity.x *= moveSpeed;
    16.                 }
    17.             }
    18.             if ( moveDirection == 1 ) // right
    19.             {
    20.                 if ( jumpEnable )
    21.                 {
    22.                     velocity.x *= moveSpeedJump;
    23.                 }
    24.                 else // this else is working and fixes ledge falloff velocity
    25.                 {
    26.                     velocity.x *= moveSpeed;
    27.                 }
    28.             }
    29.         }
    30.