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

Character Selection Question

Discussion in '2D' started by jakebaker865, Jul 10, 2020.

  1. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hi All,

    I have an issue where I have three characters in a game that are cycled through. I would actually like to put them into a UI system so the player can select which player while in game. Would anyone be able to assist with this? I haven't been able to find any way to do this. I found some tutorials on how to do this in 3d with Play Maker, but this is a 2D game and PlayMaker doesn't seem to be something I need to be able to do it. Code below.

    Code (CSharp):
    1. public GameObject[] characters;
    2.     public GameObject[] cameras;
    3.     int currentActive = 0;
    4.  
    5.  
    6.     void Update()
    7.     {
    8.  
    9.         if (Input.GetKeyDown(KeyCode.O))
    10.         {
    11.             if (currentActive == characters.Length - 1)
    12.             {
    13.                 currentActive = 0;
    14.             }
    15.             else
    16.             {
    17.                 currentActive++;
    18.             }
    19.  
    20.  
    21.             for (int i = 0; i < characters.Length; i++)
    22.             {
    23.  
    24.                 if (i == currentActive)
    25.                 {
    26.  
    27.                     characters[i].SetActive(true);
    28.                     if (currentActive == 0)
    29.                     {
    30.                         characters[i].transform.position = characters[characters.Length - 1].transform.position;
    31.                     }
    32.                     else
    33.                     {
    34.                         characters[i].transform.position = characters[i - 1].transform.position;
    35.                     }
    36.  
    37.                 }
    38.  
    39.                 else characters[i].SetActive(false);
    40.  
    41.  
    42.             }
    43.  
    44.             for (int i = 0; i < cameras.Length; i++)
    45.             {
    46.  
    47.                 if (i == currentActive)
    48.                 {
    49.  
    50.                     cameras[i].SetActive(true);
    51.                     if (currentActive == 0)
    52.                     {
    53.                         cameras[i].transform.position = cameras[cameras.Length - 1].transform.position;
    54.                     }
    55.                     else
    56.                     {
    57.                         cameras[i].transform.position = cameras[i - 1].transform.position;
    58.                     }
    59.  
    60.                 }
    61.  
    62.                 else cameras[i].SetActive(false);
    63.  
    64.  
    65.             }
    66.         }
    67.     }
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    here is a tutorial on how to create buttons

    just put your code on the buttons

     
  3. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hey thanks for the reply. The issue is the code cycles through the characters, and it doesn't let you choose them. I've followed that tutorial before. I'm looking for advice on how best to modify the code I have or come up with completely new alternative code to be able to have the character selection be done through some UI, so you can pick and choose, not just cycle through.
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    well to choose which character you want you just need to do:

    Code (CSharp):
    1. currentActive = 0; // for character 0
    2. currentActive = 1; // for character 1
    3. currentActive = 2; // for character 2
    and remove this part from your code:

    Code (CSharp):
    1. if (currentActive == characters.Length - 1)
    2.             {
    3.                 currentActive = 0;
    4.             }
    5.             else
    6.             {
    7.                 currentActive++;
    8.             }
     
  5. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36

    Ok, so I've modified my code, and added the character selects to buttons, but I have an issue where I need the characters to be setactive at the last position of the previous character. Have any thoughts on this?

    New Code:
    Code (CSharp):
    1. public GameObject characterSwitchUI;
    2.  
    3.     public GameObject Knight;
    4.     public GameObject Viking;
    5.     public GameObject Pike;
    6.  
    7.     public GameObject KnightCam;
    8.     public GameObject VikingCam;
    9.     public GameObject PikeCam;
    10.  
    11.    
    12.  
    13.     void Update()
    14.     {
    15.  
    16.  
    17.         if (Input.GetKeyDown(KeyCode.O))
    18.         {
    19.             characterSwitchUI.SetActive(true);
    20.             Time.timeScale = 0.2f;
    21.         }
    22.         else if (Input.GetKeyUp(KeyCode.O))
    23.         {
    24.             characterSwitchUI.SetActive(false);
    25.             Time.timeScale = 1f;
    26.         }
    27.      
    28.  
    29.     }
    30.  
    31.     public void KnightActive()
    32.     {
    33.    
    34.             Knight.SetActive(true);
    35.             KnightCam.SetActive(true);
    36.             Viking.SetActive(false);
    37.             VikingCam.SetActive(false);
    38.             Pike.SetActive(false);
    39.             PikeCam.SetActive(false);
    40.        
    41.     }
    42.  
    43.     public void VikingActive()
    44.     {
    45.        
    46.             Knight.SetActive(false);
    47.             KnightCam.SetActive(false);
    48.             Viking.SetActive(true);
    49.             VikingCam.SetActive(true);
    50.             Pike.SetActive(false);
    51.             PikeCam.SetActive(false);
    52.        
    53.     }
    54.  
    55.     public void PikeActive()
    56.     {
    57.        
    58.             Knight.SetActive(false);
    59.             KnightCam.SetActive(false);
    60.             Viking.SetActive(false);
    61.             VikingCam.SetActive(false);
    62.             Pike.SetActive(true);
    63.             PikeCam.SetActive(true);
    64.        
    65.     }
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you need an extra field

    Code (CSharp):
    1.  public GameObject CurrentChar;
    and then

    Code (CSharp):
    1.  
    2.     public void KnightActive()
    3.     {
    4.  
    5.             Knight.SetActive(true);
    6.             KnightCam.SetActive(true);
    7.             Viking.SetActive(false);
    8.             VikingCam.SetActive(false);
    9.             Pike.SetActive(false);
    10.             PikeCam.SetActive(false);
    11.  
    12. Knight.transform.position = CurrentChar.transform.position;
    13. CurrentChar = Knight;
    14.      
    15.     }
    16.     public void VikingActive()
    17.     {
    18.      
    19.             Knight.SetActive(false);
    20.             KnightCam.SetActive(false);
    21.             Viking.SetActive(true);
    22.             VikingCam.SetActive(true);
    23.             Pike.SetActive(false);
    24.             PikeCam.SetActive(false);
    25.      
    26. Viking.transform.position = CurrentChar.transform.position;
    27. CurrentChar = Viking;
    28.        
    29.     }
    30.     public void PikeActive()
    31.     {
    32.      
    33.             Knight.SetActive(false);
    34.             KnightCam.SetActive(false);
    35.             Viking.SetActive(false);
    36.             VikingCam.SetActive(false);
    37.             Pike.SetActive(true);
    38.             PikeCam.SetActive(true);
    39.      
    40. Pike.transform.position = CurrentChar.transform.position;
    41. CurrentChar = Pike;
    42.        
    43.     }
     
  7. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36

    Thanks man. Works!
     
    raarc likes this.