- Home /
Error With Switching Characters
So, I have this script to change characters in-game, like in Castlevania: Portrait Of Ruin, for example. It seems to work with the switching part, but when the characters change, they don't stick together. It's like this: say I move to a corner of a room. Then, I change characters and move to the other corner. Now, when I switch back, the first character appears in the previous corner, as if the game saved where it was located when the switch was made. How can I get them to stay together when I switch?
Regarding my setup: I have two characters inside a parent empty gameobject. The characters themselves have the controls script, rigidbodies, colliders, etc, and the empty gameobject has the switching script. This was the only way I could find to get it to work with the character animations. Here is the switching script:
public GameObject play1, play2;
private int characterSelect;
void Start()
{
characterSelect = 1;
play1 = GameObject.Find("Player1");
play2 = GameObject.Find("Player2");
}
void FixedUpdate()
{
if (Input.GetButtonDown("Change"))
{
if (characterSelect == 1)
{
characterSelect = 2;
}
else if (characterSelect == 2)
{
characterSelect = 1;
}
}
if (characterSelect == 1)
{
play1.SetActive(true);
play2.SetActive(false);
}
else if (characterSelect == 2)
{
play1.SetActive(false);
play2.SetActive(true);
}
}
Answer by spencersteers · Mar 29, 2018 at 11:23 AM
Once you disable a GameObject all the attached components will be disabled. Even though you have your control script on both objects it will only be active on the active GameObject.
A quick way to get the characters in the same position would be to update the deactivated players transform just before switching characters.
if (characterSelect == 1)
{
play1.transform.position = play2.transform.position;
play1.transform.rotation = play2.transform.rotation;
play1.SetActive(true);
play2.SetActive(false);
}
else if (characterSelect == 2)
{
play2.transform.position = play1.transform.position;
play2.transform.rotation = play1.transform.rotation;
play1.SetActive(false);
play2.SetActive(true);
}
That should fix the problem you are having now.
Looking into the future though you might run into similar issues like this. Ignore me if I am wrong but I'm guessing you have the same Control script on two separate GameObjects? Instead of activating / deactivating a multiple players I would recommend just enabling / disabling the visual game objects when you switch characters so that you only have 1 instance of your Control script.
Nah, this just locks the players into place and makes them sink into the floor. Can't move. But, yeah, they both have the same control script. I thought I was already activating only one at a time with the "SetActive(True/False)" thing.
Oh my bad I forgot that was inside FixedUpdate. Try moving the if else inside if (Input.GetButtonDown("Change"))
I got it to work! Here's the finished code:
public GameObject play1, play2;
private int characterSelect;
void Start()
{
characterSelect = 1;
play1 = GameObject.Find("Player1");
play2 = GameObject.Find("Player2");
}
void Update()
{
if (Input.GetButtonDown("Change"))
{
if (characterSelect == 1)
{
characterSelect = 2;
}
else if (characterSelect == 2)
{
characterSelect = 1;
}
}
if (characterSelect == 1)
{
play1.SetActive(true);
play2.SetActive(false);
play2.transform.position = play1.transform.position;
play2.transform.rotation = play1.transform.rotation;
}
else if (characterSelect == 2)
{
play1.SetActive(false);
play2.SetActive(true);
play1.transform.position = play2.transform.position;
play1.transform.rotation = play2.transform.rotation;
}
}
I think I'm going to add a cooldown timer in between changes sometime later, though. I noticed that if I rapidly switch between characters, I can sort of "glide" in mid jump and go further than a normal jump allows. Not exactly an exploit I want in my game ;) Anyway, thanks, man!
Your answer
Follow this Question
Related Questions
changing player (getting in vehicles) 3 Answers
Switching cameras (JavaScript) 0 Answers
Developing a Universal Unity Game and tips. 0 Answers
switching between in game characters 2 Answers
Changing player model with keypress. 3 Answers