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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I make my character not move when pressing both positive and negative horizontal buttons?

Discussion in 'Scripting' started by Gorgio0, Aug 28, 2022.

  1. Gorgio0

    Gorgio0

    Joined:
    Aug 28, 2022
    Posts:
    3
    This is the code that I use for my players movement:

    Code (CSharp):
    1. GetComponent<Rigidbody2D>().velocity = new Vector2(Input.GetAxis("Horizontal") * speed, Input.GetAxis("Vertical") * speed);
    So if someone could help me then that would be great!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Could always change the Input Manager to separate keys and subtract them from eachother.
     
    DevDunk likes this.
  3. Gorgio0

    Gorgio0

    Joined:
    Aug 28, 2022
    Posts:
    3
    I tried but I did not really know how you could do it, I am really new to unity.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You can go to Edit>Project Settings>Input Manager and customize your controls. What I suggested is creating two controls for reach axis, positive and negative. Then, you would read the input for each separately, and if they're both pressed, they would cancel out.
     
  5. Gorgio0

    Gorgio0

    Joined:
    Aug 28, 2022
    Posts:
    3
    I am so confused, I have no idea of how to do these things!