How do I add a feature to my player swap code that lets you control both players at once?

I have a game wherein there are 2 playable characters, and you can switch between them for different things. I’m extremely new to coding and Unity Hub as a whole, so I used a tutorial to show me how to make a player swap code, and it looks like this:

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

public class PlayerSwitch : MonoBehaviour
{
    public PlayerMovement playerMovement;
    public PlayerMovement player2Movement;
    public bool player1Active = true;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            SwitchPlayer();
        }
    }

    public void SwitchPlayer()
    {
        if (player1Active)
        {
            playerMovement.enabled = false;
            player2Movement.enabled = true;
            player1Active = false;
        }
        else
        {
            playerMovement.enabled = true;
            player2Movement.enabled = false;
            player1Active = true;
        }
    }
}

This code lets you switch between players, but I don’t know what to add for you to be able to play both at once at the press of a key, or if it’s something I have to change in Unity rather than change the code.

Would some code like this work? It should make both players active when space is pressed. Hope this is what you’re looking for.

:slight_smile: I’m actually making a game around a mechanic similar to this.

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

public class PlayerSwitch : MonoBehaviour
{
    public PlayerMovement playerMovement;
    public PlayerMovement player2Movement;
    public bool player1Active = true;

    private bool bothPlayersMode = false;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            SwitchPlayer();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            bothPlayersMode = !bothPlayersMode;
        }
    }

    public void SwitchPlayer()
    {
        if (bothPlayersMode)
        {
            playerMovement.enabled = true;
            player2Movement.enabled = true;
            player1Active = false;
        }
        else if (player1Active)
        {
            playerMovement.enabled = false;
            player2Movement.enabled = true;
            player1Active = false;
        }
        else
        {
            playerMovement.enabled = true;
            player2Movement.enabled = false;
            player1Active = true;
        }
    }
}