I’m having a few problems in game because I have two players which share the class PlayerSetup and there are some variables that are individual for each player (e.g. velocity). When it’s player one’s turn and he changes his velocity from x to y, player two’s velocity also changes to y.
I want to know how can I manipulate both velocitys as different things although the two objects have the same class.
My code looks like the first one, but let’s say I want to do something like this on PlayerSetup:
void Update () {
if (Input.GetKey(KeyCode.D)) {
velocity += 1.0f;
}
if (Input.GetKey(KeyCode.A)) {
velocity -= 1.0f;
if (velocity < 1.0f)
velocity = 1f;
}
}
Both players have the PlayerSetup class in the same scene but when the player types “D” or “A”, how can I define which velocity will increase (or decrease)? The way I did changed both velocities.
I imagined a conditions statement like “if it’s player one’s turn, change his velocity and his velocity only” but I don’t know how is it possible to do this without changing player two’s velocity too.
You need logic in there that reads like: If my turn, proceed. Else do nothing. What is happening is both objects are calling Input.GetKey in their update method, and each object is increasing its own velocity because the key is down. You only want that code to be run whenever it’s that player’s turn. You need an additional script that manages whose turn it is. Make a public bool “isMyTurn” in your player class and have the new script manage that value. Then at the beginning of your player Update() function, add “if (!isMyTurn) return;”
I was using that logic to define which turn is it but when I change the velocity on player one’s turn, the velocity is changed when player two’s turn starts.