Search code examples
unity-game-engineinput

Player input not working properly in unity


I'm attempting to recreate aspects of The Binding of Isaac in Unity (just as a learning exercise, not the entire game). I've created a script to enable Isaac to move using the WASD keys and to shoot tears with the arrow keys. While the WASD movement works perfectly, pressing the arrow keys causes Isaac to both move and shoot at the same time. Ideally, the arrow keys should only be used to fire tears and not to move Isaac. I'm seeking assistance to resolve this issue. Apologies if this seems like a simple question, and sorry for bad english

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IsaacMovements : MonoBehaviour
{
    public float speed = 40f;
    public Rigidbody2D projectilePrefab;
    public float projectileSpeed = 10f;

    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        if (rb == null)
        {
            Debug.LogError("Rigidbody2D component is not attached to the GameObject.");
        }
    }

    // Update is called once per frame
    void Update()
    {
        Move();

        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow) ||
            Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            Shoot();
        }
    }

    void Move()
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");

        Vector2 movement = new Vector2(horizontalInput, verticalInput).normalized;
        rb.velocity = movement * speed;
    }

    void Shoot()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            Fire(Vector2.left);
        else if (Input.GetKey(KeyCode.RightArrow))
            Fire(Vector2.right);
        else if (Input.GetKey(KeyCode.UpArrow))
            Fire(Vector2.up);
        else if (Input.GetKey(KeyCode.DownArrow))
            Fire(Vector2.down);
    }

    void Fire(Vector2 direction)
    {
        Rigidbody2D projectileInstance = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
        projectileInstance.velocity = direction * projectileSpeed;
    }
}

I tried changing the code extensively throughout the process, but nothing seemed to work. I also attempted to use different keys for shooting instead of the arrow keys, but I still didn't get the desired result.


Solution

  • This happens because you are using Input.GetAxisRaw("Horizontal"), In the Unity editor, Horizontal Axis is Defined as Both A,D And Left,Right Keys, There are a few solutions, Two of the easiest are as such:

    1. Change GetAxisRaw, Use the specific Keys you want for each direction (This is kind of a lazy solution)

    2. In the editor prefrences, change the axis keys to whichever you would like over at Edit > Project Settings > Input Manager > Axes > Horizontal / Vertial > Button + Alt Buttons