- Home /
How to define a GameObject for both players?
Hello guys! I'm searching for a while on how it is supposed to work for a given object(which would be a character) to be controlled by 2 players.
The very same thing that happens in a fighting game where both players can select and control the same characte. I have seem something with the PreFab and the tag index (arrays) but I've been facing some problems ... it's ok when I code it very straight forward like:
if(Input.GetKeyDown(KeyKode.A))//Player one
{
player[0].rigidbody2D.velocity = new Vector2(2f, 0);
}else
if(Input.GetKeyDown(KeyKode.B))// PLayer two
{
player[1].rigidbody2D.velocity = new Vector2(2f, 0);
}
But the problem comes around when I try to put it into a more organized code like the following:
Note: this is found in a baseclass
public void Vel(float X, float Y) {
if(player[0].tag == "Player")
}
player[0]. rigidbody2D.velocity = new Vector2(X, Y);
}else
if(player[1].tag == "Player")
{
player[1].rigidbody2D.velocity = new Vector2(X, Y);
}
}
And then call the Vel method in the update method in a child class...
if(player[0].tag == "Player" && Input.GetKeyDown(KeyCode.B))
{
Vel(1f, 0);
}
if(player[1].tag == "Player" && Input.GetKeyDown(KeyCode.N))
{
Vel(1f, 0);
}
It always trigger the first "if" condition. No matter if I switch to player 2 code it always trigger only the first if. I want to know if there is a specific way to build a multiplayer(offline) controller detection.
Thanks in advance.
I've done that already in fact remove else wouldn't help in this case. The problem is that both players get info from the same script. I just want to know the right way to build a solid multiplayer system.
Thank you!