Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character Switching System

Discussion in 'Scripting' started by Android272, Dec 13, 2016.

  1. Android272

    Android272

    Joined:
    Oct 14, 2016
    Posts:
    22
    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
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum Buttons {
    5.     Right,
    6.     Left,
    7.     Up,
    8.     Down,
    9.     A,
    10.     B,
    11.     SwitchCharacter
    12. }
    13.  
    14. public enum Condition {
    15.     GreaterThan,
    16.     LessThan
    17. }
    18.  
    19. [System.Serializable]
    20. public class InputAxisState {
    21.     public string axisName;
    22.     public float offValue;
    23.     public Buttons button;
    24.     public Condition condition;
    25.  
    26.     public bool value {
    27.         get {
    28.             var val = Input.GetAxis(axisName);
    29.  
    30.             switch(condition) {
    31.             case Condition.GreaterThan:
    32.                 return val > offValue;
    33.             case Condition.LessThan:
    34.                 return val < offValue;
    35.             }
    36.  
    37.             return false;
    38.         }
    39.     }
    40. }
    41.  
    42. public class InputManager : MonoBehaviour {
    43.     public InputAxisState[] inputs;
    44.     public InputState inputState;
    45.  
    46.     void Update () {
    47.         foreach (var input in inputs) {
    48.             inputState.SetButtonValue(input.button, input.value);
    49.         }
    50.     }
    51.  
    52.     public void SetInputState(GameObject character) {
    53. //        inputState = character;
    54.     }
    55. }
    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.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    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:
    Code (csharp):
    1.  
    2. public class InputManager : MonoBehaviour {
    3.     public InputAxisState[] inputs;
    4.     public InputState inputState;
    5.    
    6.     public InputState player1;
    7.     public InputState player2;
    8.  
    9.     void Update () {
    10.         if(Input.GetButtonDown("SwapPlayer"))
    11.         {
    12.             if(inputState == player1)
    13.                 inputState = player2;
    14.             else
    15.                 inputState = player1;
    16.         }
    17.    
    18.         foreach (var input in inputs) {
    19.             inputState.SetButtonValue(input.button, input.value);
    20.         }
    21.     }
    22.  
    23.     public void SetInputState(GameObject character) {
    24. //        inputState = character;
    25.     }
    26. }
    27.  
     
  3. Android272

    Android272

    Joined:
    Oct 14, 2016
    Posts:
    22
    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.