- Home /
How do I control which class I'm accessing?
I'm making a couch co-op game. This is my first time attempting multiplayer so this should be an easy question.
I've got a script called InputManager
. Inside of InputManager
are 2 public static classes: Player1
and Player2
. Inside of those classes are variables associated to the corresponding controller.
This game also contains vehicles. Each vehicle has a script that controls its movement and this is how they would access variables from the InputManager: InputManager.Player1.jump
or InputManager.Player2.jump
.
However my problem is, I only want to read the input from the player that is currently driving the vehicle, something like this: curPlayer.jump
How do I do this?
Can no one give me a simple solution to this problem?
Answer by Bunny83 · Sep 26, 2016 at 12:47 AM
You can't with static classes.
That's the reason why we actually have instances of classes, so we can reference them. What's actually the difference between Player1 and Player2? Usually you would have a Player class and then have an instance for player1 and one for player2. If the code for those should be different you could create seperate subclasses for each and create one instance for each player.
Can you post some actual code how your Player1 / 2 class looks like? Do they even contain methods / properties or only fields?
public class Input$$anonymous$$anager : $$anonymous$$onoBehaviour {
void Update ()
{
Player1.ReadInput();
if (Player1.restart)
Scene$$anonymous$$anager.LoadScene(0);
if (Player1.Quit)
Application.Quit();
Player2.ReadInput();
}
public static class Player1
{
public static Vector2 dir;
public static float Thrust;
public static float zoom;
public static bool restart, Quit;
public static void ReadInput()
{
dir.x = Input.GetAxis("Horizontal1");
dir.y = Input.GetAxis("Vertical1");
if (dir.magnitude > 1)
dir.Normalize();
zoom = Input.GetAxis("Zoom1");
Thrust = Input.GetAxis("Thrust1");
Thrust = (Thrust + 1) / 2;
//Player1 privilege only!
restart = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Joystick1Button3);
Quit = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Joystick1Button9);
}
}
public static class Player2
{
public static Vector2 dir;
public static float Thrust;
public static float zoom;
public static bool leave;
public static void ReadInput()
{
dir.x = Input.GetAxis("Horizontal2");
dir.y = Input.GetAxis("Vertical2");
if (dir.magnitude > 1)
dir.Normalize();
zoom = Input.GetAxis("Zoom2");
Thrust = Input.GetAxis("Thrust2");
Thrust = (Thrust + 1) / 2;
//Player2 privilege only!
leave = false;
}
}
}
So ins$$anonymous$$d of having to do this:
if(isPlayer1)
Input$$anonymous$$anager.Player1.jump
else if(isPlayer2)
Input$$anonymous$$anager.Player2.jump
I would do something like this:
if(isPlayer1)
curPlayer = Input$$anonymous$$anager.Player1;
else if(isPlayer2)
curPlayer = Input$$anonymous$$anager.Player2;
curPlayer.jump;
Your answer
Follow this Question
Related Questions
Recognize number of players and set differents input for an action 0 Answers
how can I assign and use different controller schemes for a single keyboard? 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Is Unity able to utilize 10 gamepads at once? 2 Answers
Multiplayer Estruture 1 Answer