- Home /
Switch Players during play
I'm making a 2d platformer game and I want to be able to swap characters during play with the click of a button. All my characters are animated (Walk, run, jump etc.) and will have different attack moves and attributes. My main player holds a UI/game manager so I would also need this to travel between scenes with players. I'm guessing I need to make an array and deactivate each player etc but I haven't been able to figure this one out, Any help or advice would be much appreciated. I'm pretty new to this
If its 2d then you can use SpriteRendere.sprite = newsprite.
Answer by willwdaniels87 · Mar 21 at 06:13 AM
Got it working sort of with the following script but my animations of the two different players clash.
public class PlayerSwitch : MonoBehaviour { public GameObject play1, play2; private int characterSelect;
void Start()
{
characterSelect = 1;
play1 = GameObject.Find("Buck");
play2 = GameObject.Find("Bill");
}
void Update()
{
if (Input.GetButtonDown("Fire3"))
{
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;
}
}
}
Any help with the animation controller issue? My UI and scene loading is also playing up but I think I'll be able to solve that issue.
Answer by pr_0_0_ · Mar 21 at 07:35 AM
Why are you making the character which is to disappear move and rotate? Shouldn't it be the other way round? Also I'm not sure about this, but probably it will be better if you put the SetActive (false) in the end, so that the transformation happens before the character disappears. Try this:
if (characterSelect == 1)
{
play1.SetActive(true);
play1.transform.position = play2.transform.position;
play1.transform.rotation = play2.transform.rotation;
play2.SetActive(false);
}
else if (characterSelect == 2)
{
play2.SetActive(true);
play2.transform.position = play1.transform.position;
play2.transform.rotation = play1.transform.rotation;
play1.SetActive(false);
}
Your answer
Follow this Question
Related Questions
so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers
Having some stuck issues on the 2D infinite runner 0 Answers
,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers
Left/Right movement 2D game,Why is this code to make my character move left/right not working? 0 Answers
How to make animations transition based on players direction and speed ? 0 Answers