I would like to know how to get Unity joystick values

Viewed 25

I don't know how to make player move from joystick in Unity 2D.

[SerializeField] private float speed;
private Rigidbody2D rb;
private Animator anim;
private bool grounded;

private void Awake()
{
    Time.timeScale = 1;
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
}

void Update()
{
    Move();   
}

private void Move()
{
    //Move
    float horizontalInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
    
    //Flip
    if (horizontalInput > 0f)
    {
        transform.localScale = new Vector3(6, 6, 6);
    }
    else if (horizontalInput < 0f)
    {
        transform.localScale = new Vector3(-6, 6, 6);
    }
    
    anim.SetBool("run", horizontalInput != 0);
    anim.SetBool("grounded",grounded);
}

private void OnCollisionEnter2D(Collision2D col)
{
    if (col.gameObject.tag == "Ground")
    {
        grounded = true;
    }
    else if (col.gameObject.tag == "Skill")
    {
        grounded = true;
        Destroy(col.gameObject,5f);
    }
}

This is code I want to modify to get the joystick. It's not complete as I still don't know how to use joystick in code.

enter image description here

0 Answers
Related