Character Switching System

So I am trying to make a system that will allow my players to switch between two characters like in Donkey Kong Country. like Donkey I have two players on screen, one character following the one who the player is controlling. So I want to switch control of the two characters without destroying one and making the other appear in its place or simply turning off the characters Mesh Renderer.

Bellow in the Inspector is my InputManager. This InputManager takes the Players inputs and sends them to the player game object. At the bottom fo this script, I have an Input State field that tells my game wich game object to send the players inputs to. Or at least that is how I understand it, I used a tutorial to get this setup. If I replace Player with Player2 while playing I gain control of Player Two

Here is this InputManager script

using UnityEngine;
using System.Collections;

public enum Buttons {
    Right,
    Left,
    Up,
    Down,
    A,
    B,
    SwitchCharacter
}

public enum Condition {
    GreaterThan,
    LessThan
}

[System.Serializable]
public class InputAxisState {
    public string axisName;
    public float offValue;
    public Buttons button;
    public Condition condition;

    public bool value {
        get {
            var val = Input.GetAxis(axisName);

            switch(condition) {
            case Condition.GreaterThan:
                return val > offValue;
            case Condition.LessThan:
                return val < offValue;
            }

            return false;
        }
    }
}

public class InputManager : MonoBehaviour {
    public InputAxisState[] inputs;
    public InputState inputState;

    void Update () {
        foreach (var input in inputs) {
            inputState.SetButtonValue(input.button, input.value);
        }
    }

    public void SetInputState(GameObject character) {
//        inputState = character;
    }
}

I want to write a script that will switch out this game object with another when the player presses some button. But I am new to Unity and Game programming and have no idea how I would go about doing this? Any suggestions about where to go from here? If you need any more code for more context just ask.

Well in Donkey Kong Country, both characters are on screen at the same time always. One just following the other. When you press the appropriate button, they just swap back and forth.

You mentioning turn off players makes me wonder, what is your desired out come? Is only one character on screen at a time?

Here’s the reason I ask… if the characters are one screen both at a time like in DKC, there’s actually a bit more work that’ll need to be done for their movement logic making sure the play only has control over one at a time… which can be done numerous ways. This has implications on the scripting, and effects the complexity… I could come up with a system that handles either or, but would that be more work than necessary?

OK… so you have a system for this already.

That’s cool.

OK… all you need to do is replace the ‘InputState’ reference with the current player.

Have a script that listens for inputs, it should have a reference to BOTH players, and a reference to this InputManager (heck you could write this IN the InputManager).

Then on given button press, just swap out the referenced ‘InputState’.

Though I must say… your InputAxisState is a bit limited. It doesn’t have a ‘Down’ ‘Held’ ‘Release’ states… it’s just on or off.

So I’m going to use unity’s built in Input system:

public class InputManager : MonoBehaviour {
    public InputAxisState[] inputs;
    public InputState inputState;
   
    public InputState player1;
    public InputState player2;

    void Update () {
        if(Input.GetButtonDown("SwapPlayer"))
        {
            if(inputState == player1)
                inputState = player2;
            else
                inputState = player1;
        }
   
        foreach (var input in inputs) {
            inputState.SetButtonValue(input.button, input.value);
        }
    }

    public void SetInputState(GameObject character) {
//        inputState = character;
    }
}

Sorry for the confusion I do want both characters on the screen at the same time, and when you press a button the camera focuses on the other character and you gain control of them. I will figure out the path finding to make the characters follow each other later. I found a form post with an answer saying to turn off each character when you switch them but I don’t want to do that. I should have just not said anything about it

I did find a small bug. If you are pressing left arrow for example while you press the swap key the character you are swapping from will continue moving left after you have swapped characters.