- Home /
2D Character Switch Systsem (Same Location)
Hello developers;
Looking for some insight as to how some of you have implemented a character switch into your game. I have 3 characters that I need to always remain at the same position as eachother, and have the ability for a player to switch between any of the 3 by pressing a button on the gui. There would of course be three buttons and when one character becomes visible, the other 2 are not.
Keep in mind that this is in a 2d environment, meaning that a camera workaround won't cut it.
Thanks for any help.
Answer by Destran · Mar 07, 2014 at 04:08 AM
Make all three character sprites a child to a single empty gameobject so that their positions and what not all remain the same. Then enable/disable their renderers.
So I'm pretty new to scripting but I'll try an example.
#pragma strict
var character1 : Renderer; // Your first character
var character2 : Renderer; // Your second character
var character3 : Renderer; // your third character
var activechar = 0; // when 0 active character is char1, when 1 active character is char2, etc.
function Start()
{
activechar = 0;
character1.enabled = true;
character2.enabled = false;
character3.enabled = false;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.E))//if they press E the active char is switched
{
activechar++;
if(activechar >2)
activechar = 0; // makes sure it cycles through your 3 characters
}
IsActive(); //calls the IsActive function to see which sprite is enabled
}
function IsActive()
{
if(activechar==0) //shows only char 1
{
character1.enabled = true;
character2.enabled = false;
character3.enabled = false;
}
if(activechar == 1) //shows only char 2
{
character1.enabled = false;
character2.enabled = true;
character3.enabled = false;
}
if(activechar == 2) //shows only char 3
{
character1.enabled = false;
character2.enabled = false;
character3.enabled = true;
}
}
Drag your 3 sprites onto the renderer areas in your empty gameobject in the inspector thing to match the char1/char2/char3. There might be a better way to do this, but I'm pretty new to Unity so I'm still learning. Hope this works!
Thanks for the help. I will try to work with this and modify it to suit my needs.
Your answer
Follow this Question
Related Questions
switch player and npc follow player 0 Answers
2D example: Flip character without moving 0 Answers
Unity 2D Top Down Jump 1 Answer
2D Character Flip 2 Answers
changing player (getting in vehicles) 3 Answers