Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to put a direction of the joystick in a script?

Discussion in 'Input System' started by Peilo77, Nov 18, 2020.

  1. Peilo77

    Peilo77

    Joined:
    Aug 18, 2020
    Posts:
    18
    Hi there,

    I'm really a beginner at Unity and sorry if this is common knowledge. I searched for the answer but didn't find it.

    I'm in the process of creating a 2D-side scrolling shoot'em up and want the spaceship to do an animation when it goes up and down by using the gamepad movement stick.

    It works absolutely fine while using the arrows on the keyboard but I can't "transpose" this code on the gamepad because I do not know nor find the correct script/coding words.

    Here is my movement function:

    void MovePlayer()
    {
    if (canMove == true)
    {
    if (Input.GetAxisRaw("Vertical") > 0f)
    {
    Vector3 temp = transform.position;
    temp.y += speed * Time.deltaTime;

    transform.position = temp;
    }
    else if (Input.GetAxisRaw("Vertical") < 0f)
    {
    Vector3 temp = transform.position;
    temp.y -= speed * Time.deltaTime;

    transform.position = temp;
    }

    if (Input.GetAxisRaw("Horizontal") > 0f)
    {
    Vector3 temp = transform.position;
    temp.x += speed * Time.deltaTime;

    transform.position = temp;

    }
    else if (Input.GetAxisRaw("Horizontal") < 0f)
    {
    Vector3 temp = transform.position;
    temp.x -= speed * Time.deltaTime;

    transform.position = temp;
    }
    }

    }

    In the Update function I have this for the animations that work well with the arrow keys:

    void Update()
    {


    if (Input.GetKey(KeyCode.UpArrow))
    {
    anim.SetBool("IsMovingUP", true);
    }
    else
    {
    anim.SetBool("IsMovingUP", false);
    }

    if (Input.GetKey(KeyCode.DownArrow))
    {
    anim.SetBool("IsMovingDOWN", true);
    }
    else
    {
    anim.SetBool("IsMovingDOWN", false);
    }

    MovePlayer();
    }

    Now does someone know what the coding/scripting words are for the gamepad movement stick when moving UP or DOWN so I could do for example for the UP movement something like:

    if (Input.GetKey(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.JoystickMOVEMENTUP???))

    Many thanks in advance for your help on this and sorry if I'm in the wrong section or if it's common knowledge and I'd appreciate any help on this.
     
  2. Peilo77

    Peilo77

    Joined:
    Aug 18, 2020
    Posts:
    18
    Oh man I really suck LOL the solution was so simple and found it out LOL

    if (Input.GetAxisRaw("Vertical") > 0f)
    {
    anim.SetBool("IsMovingUP", true);
    }
    else
    {
    anim.SetBool("IsMovingUP", false);
    }

    if (Input.GetAxisRaw("Vertical") < 0f)
    {
    anim.SetBool("IsMovingDOWN", true);
    }
    else
    {
    anim.SetBool("IsMovingDOWN", false);
    }

    This thread can be closed :)