Switching between 2 characters on the fly?

I have a mechanic I want to create, where on Input.GetButtonDown i can switch between two different characters, with different “abilities” but I want to be able to do this on the fly as in:
You jump in the air with character A, press a button to swap to character B in midair, use B’s ability, then swap again to A before you land back on the ground. How do I go about doing that best?
Kind of like when Mario eats a Fire Flower and changes characters but his position in the game remains the same.
I tried this:
public GameObject tiny;
public GameObject tall;

void Update () {

if (Input.GetButtonDown (“Switch”)) {

if(tiny.activeInHierarchy)
{
if(tall.activeSelf != true)
{
tiny.SetActive(false);
tall.SetActive(true);
}
}else{
if(tall.activeInHierarchy)
{
if(tiny.activeSelf != true)
{
tall.SetActive(false);
tiny.SetActive(true);
}
}
}
}
}

But this obviously turns off the game object all together so I would have two seperate jumps and moves when I switch.
Do I need to make an empty player object and then “Instatiate” the character I want after each press at the location of the player parent object? And then script a movement code for the parent to walk around just essentially swapping the looks instead of the actual prefab? I dont have any art skills so I’m just using square game objects as the players in the moment! idk how to do that and it sounds complicated so before I try hard to make a mess outta my code I wanted to ask what the best way would be! ^^’

(PS:I dont have any art skills so I’m just using square game objects as the players in the moment!
PPS: this is my first game so I’m completely new to this. Do you think this is a bit too complicated for a first game?)

I would arrange both versions of the player as children of an (otherwise empty) GameObject parent. So in other words, your scene hierarchy will look something like this:

  • Player (no geometry here)

  • TallVersion (“tall” model and animations)

  • ShortVersion (“short” model and animations)

Now you can easily activate/deactivate the different versions within Player. Most of the scripts would be on Player too, though you could have some custom components on each of the versions for where they do different things.

The only fly in the ointment here might be physics… if you have separate colliders & rigidbodies on the different versions, then when you switch from one to another, you would need to be sure to set the velocity of the new one equal to the old one. But provided you do that, I think even physics should work pretty well.

1 Like

Ahh so put the player controller on the Player Parent not the children will fix my problem…I’m not thinking smart and programmy enough I can tell by something that simple ._.

Regarding the Physics I want their gravity and jump height and stuff to be the same so that shouldn’t be a problem right?

Thanks tho I’ll try that as soon as my unity stops crashing all the dang time Q_Q

1 Like