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

Switching 3 different (unique) Characters in Same position

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

  1. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hey All, Needing some direction on this.

    I have 3 characters that I need to be able to switch through while the game is playing. They each have different player movement scripts with their own unique abilities, health, and stats.

    Can anyone think of a way to accomplish this? I've been searching for a few days with no luck. Most of what I come across is switching renderers or having all 3 active and the camera switches between the 3.

    I thought about an array of the prefabs for the chraracters that you could SetActive during runtime, but that would present the challenge of having each one be able to be active in the same position as the previous character sprite.
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    make 3 separate objects with each character script, you can just setactive + set transform.position to the current pos and deactivate the current one when you want to switch. What you describe seems pretty easy to do.
     
  3. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Thanks for the reply. I've been stuck on this for a while, and I'm not really getting anywhere. It may be easy for someone who knows alot more code than me, but I'm pretty new to this. \

    What I have so far is Gameobjects that are prefabs in an array that I'm trying to cycle through. Can't seem to get farther than that though.
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you dont need to cycle to an array or use prefabs.

    1) Get your 3 characters in one scene all at the same time.
    2) Create a 4th object and attach a new script called charmanager(or anyhthing else) and then add
    Code (CSharp):
    1.  
    2. public GameObject currentChar;
    3. public GameObject charA;
    4. public GameObject charB;
    5. public GameObject charC;
    3) go into the inspector and drag your characters into these fields
    4) here is an example code you put on your charmanager script to switch to character C by pressing the F key:
    Code (CSharp):
    1.  
    2.  
    3. void Awake(){
    4. currentChar = charA;
    5. charC.SetActive(false);
    6. charB.SetActive(false);
    7. }
    8.  void Update()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.F))
    11.         {
    12.    
    13. charA.SetActive(false);
    14. charB.SetActive(false);
    15. charC.transform.position = currentChar.transform.position;
    16. charC.SetActive(true);
    17. currentChar = charC;
    18.  
    19.         }
    20.  
    21.  
    22. }
    23.  
     
  5. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36

    I actually came up with the code below for cycling through the characters, which works, but I'm unsure how to get the activated character at the new position of the previous character:

    Code (CSharp):
    1.    public GameObject[] characters;
    2.     public GameObject[] cameras;
    3.     int currentActive = 0;
    4.  
    5.     void Update()
    6.     {
    7.         if (Input.GetKeyDown(KeyCode.O))
    8.         {
    9.             if (currentActive == characters.Length - 1) currentActive = 0;
    10.             else currentActive++;
    11.             for (int i = 0; i < characters.Length; i++)
    12.             {
    13.                 if (i == currentActive) characters[i].SetActive(true);
    14.                 else characters[i].SetActive(false);
    15.             }
    16.  
    17.             for (int i = 0; i < cameras.Length; i++)
    18.             {
    19.                 if (i == currentActive) cameras[i].SetActive(true);
    20.                 else cameras[i].SetActive(false);
    21.             }
    22.         }
    23.     }
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    well its

    Code (CSharp):
    1. characters[i].transform.position = characters[i-1].transform.position;
    you'll have to make that i-1 wrap around if i-1<0
     
  7. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Awesome, thanks. Confused by what you mean by wrap around?
     
  8. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    same thing you are doing here

    Code (CSharp):
    1.    if (currentActive == characters.Length - 1) currentActive = 0;
    2.             else currentActive++;
     
  9. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Sorry man, Still confused. Are you saying that I need to have another if else statement just for the transform.position?
     
  10. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    no i mean that when you do the operation

    i - 1

    if i is = 0

    0 - 1 = -1

    however you dont have a characters[-1] this will give you an out of range exception

    so you want this value to loop around to your maximum character index

    so when i is < 0 you have to make it characters[characters.length -1]

    in other words when you get characters[-1] you need it to go to characters[2]
     
  11. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Man, this is hurting my head. I'm just completely lost by what you're saying. i - 1 only happens in the code you gave me, unless you're talking about characters.Length - 1, in which I have no idea how to wrap i - 1 back to the original so characters never gets to "-1"


    Code (CSharp):
    1. for (int i = 0; i < characters.Length; i++)
    2.             {
    3.  
    4.                 if (i == currentActive)
    5.                 {
    6.                    
    7.                     characters[i].SetActive(true);
    8.                     characters[i].transform.position = characters[i - 1].transform.position;
    9.                 }
    10.  
    11.                 else characters[i].SetActive(false);
    12.  
    13.                
    14.             }
     
  12. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    yes if your current character is character 0, in other words currentActive = 0, you will get a bug

    i might aswell just edit that code, here:

    Code (CSharp):
    1.     for (int i = 0; i < characters.Length; i++)
    2.                 {
    3.    
    4.                     if (i == currentActive)
    5.                     {
    6.                      
    7.                         characters[i].SetActive(true);
    8.         if (currentActive==0)
    9.                     {    
    10. characters[i].transform.position = characters[characters.Length - 1].transform.position;
    11.   }else        
    12. {    
    13. characters[i].transform.position = characters[i - 1].transform.position;
    14.   }
    15.                    
    16.                     }
    17.    
    18.                     else characters[i].SetActive(false);
    19.    
    20.                  
    21.                 }
    22.  
     
  13. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36

    First off, thank you for being so patient with me. I'm still learning.

    I now get what you mean by having to wrap the transform.position in a loop, not the entire loop itself. I'm not 100% that I would be able to recreate this down the road, but this helps me a ton!
     
  14. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    no problem just a mention that you had already had figured this out by yourself in this part of your code

    Code (CSharp):
    1.        if (currentActive == characters.Length - 1) currentActive = 0;
    2.             else currentActive++;
    this forces your current active to wrap around to 0 if it would become higher than 2
     
  15. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Thanks man. It was simply a case of I knew what to do, but didn't know where to do it, which usually costs me a day or two of trying things in different places and never getting anywhere.