Search Unity

Joystick Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Mar 17, 2020.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Hey I need some help. I have managed to make the joystick and the buttons to working. But there is one problem, my walking animations won't work unless I have the directional inputs for the joystick. I know the input involves the x and y axis functions on the controller, but how do I put in the values?
    Here is the script piece:
    Code (CSharp):
    1.     //Movement Animation
    2.         if (Input.GetKey(KeyCode.W))
    3.         {
    4.             animator.SetBool("Forward bool", true);
    5.         }
    6.         //else if (Input.GetKey())
    7.         //{
    8.         //    animator.SetBool("Forward bool", true);
    9.         //}
    10.         else if (Input.GetKey(KeyCode.A))
    11.         {
    12.             animator.SetBool("Forward bool", true);
    13.         }
    14.         else if (Input.GetKey(KeyCode.D))
    15.         {
    16.             animator.SetBool("Forward bool", true);
    17.         }
    18.         else
    19.         {
    20.             animator.SetBool("Forward bool", false);
    21.         }
    22.  
    23.         if (Input.GetKey(KeyCode.S))
    24.         {
    25.             animator.SetBool("Forward bool", false);
    26.             animator.SetBool("Backward bool", true);
    27.         }
    28.         else
    29.         {
    30.             animator.SetBool("Backward bool", false);
    31.         }
    The commented pieces are the lines where if you tilt the stick left. Any help is appreciated.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Look into Input.GetAxis. If the value for 'Horizontal' is less than -0.5, move left, if it's greater than 0.5 move right. If it's between -0.5 and 0.5 don't do anything.
     
  3. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Tried it. Works flawlessly. Thanks a bunch.