Control 2 characters separately using WASD and arrow keys.

Hi, I’m brand new to coding and Unity.

I’m trying to create a 2 player game where each player uses different keys to move, ideally WASD for one and arrow keys for other.

So far I’ve had no luck finding information on how to do this…

I would actually check with the specific key instead, like:

if (Input.GetKey(KeyCode.W){
...
}

Just split the built-in inputs (in Assets > Project > Input) of ‘Vertical’ and ‘Horizontal’ by assigning only w/s (‘Vertical’) or arrow-up/down (‘Vertical1’) and a/d (‘Horizontal’) or arrow-left/right (‘Horizontal1’), finally handle these inputs to move the different characters.

Hi

Important is to check for user inputs in Update method because it runs faster than other update methods and is more reliable.

You have plenty of ways how to solve this.


1. First is to use specified keys for every player

This is a simple solution and quick, but not so scalable because you have hardcoded keys to your code so you can’t change it in Unity default input settings, or it would be slightly harder to implement something like options menu for this kind of solution. But its kinda fast and it’s how it’s done if you would program your game from scrap.

public class TwoPlayerInputs : MonoBehaviour {

    //Player Objects
    [SerializeField]
    private GameObject Player1;
    [SerializeField]
    private GameObject Player2;


	void Update () {
        //Check if players are assigned
	    if(Player1 && Player2)
        {
            //Processing Player1 inputs
            if (Input.GetKey(KeyCode.A))
            {
                //Moves Player1
                //something like this
                Player1.transform.position = new Vector3(Player1.transform.position.x + 1, Player1.transform.position.y, Player1.transform.position.z);
            }
            if (Input.GetKey(KeyCode.D))
            {
                //Moves Player1
            }
            if (Input.GetKey(KeyCode.W))
            {
                //Moves Player1
            }
            if (Input.GetKey(KeyCode.S))
            {
                //Moves Player1
            }

            //Processing Player2 inputs
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                //Moves Player2
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                //Moves Player2
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                //Moves Player2
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                //Moves Player2
            }
        }	
	}
}

2. Second is to use Unity Input axis

This is like the Unity way. It uses default inputs which you can modify in the editor or after building your app. It is very simple to make settings menu with this way and it is much more flexible. It even supports like Gamepad Inputs and has much more settings for example deadzones, sensitivity, gravity and other.
It is almost the same as the first example, but better, because you can use direct input values from the axes.

public class TwoPlayerInputs : MonoBehaviour {

    //Player Objects
    [SerializeField]
    private GameObject Player1;
    [SerializeField]
    private GameObject Player2;


	void Update () {
        //Check if players are assigned
	    if(Player1 && Player2)
        {
            //Processing Player1 input for horizontal 
            //This does the trick: Input.GetAxis("HorizontalPlayer1")
            Player1.transform.position = new Vector3(Player1.transform.position.x + Input.GetAxis("HorizontalPlayer1"), Player1.transform.position.y, Player1.transform.position.z);
            //Processing Player1 input for vertical 
            Player1.transform.position = new Vector3(Player1.transform.position.x, Player1.transform.position.y + Input.GetAxis("VerticalPlayer1"), Player1.transform.position.z);

            //Processing Player2 input for horizontal 
            //This does the trick: Input.GetAxis("HorizontalPlayer2")
            Player2.transform.position = new Vector3(Player2.transform.position.x + Input.GetAxis("HorizontalPlayer2"), Player2.transform.position.y, Player2.transform.position.z);
            //Processing Player2 input for vertical 
            Player2.transform.position = new Vector3(Player2.transform.position.x, Player2.transform.position.y + Input.GetAxis("VerticalPlayer2"), Player2.transform.position.z);
        }
    }
}

It is very important to set up the axis in project settings

It is under Edit>ProjectSettings>Input

Here are axes and you need to modify their names to match axis names in your code. Add negative and positive buttons for example like A and D. And than you are ready to go.


Hope it helped :slight_smile:

I would suggest you watch this video

or go through the smaller videos for beginner scripting tutorials since this is quite an easy problem to solve after you learn some really basic scripting you can also watch this tutorial series [Archived] Space Shooter - Unity Learn
it teaches you pretty much all you need to know to at least know where to start in unity

I hope this helps you good luck