I am working on a game and I am totally stuck on what to do here. My problem is that I want a turn based combat for my game like a pool game or tic tac toe or anything. I am working on local multiplayer where screen needs to be divided in two parts. Upper part for player1 and lower part for player2, So far I am also not able to do that. Each player will get 5 turns. Player will take turn one by one until all 5 are taken.
The controller script on my both object is :
Vector2 startPos, endPos, direction;
Rigidbody2D myRigidbody2D;
public float Power = 10f;
public float maxDrag = 5f;
void Start() {
myRigidbody2D = GetComponent<Rigidbody2D> ();
}
void OnMouseDown() {
if (Input.GetMouseButtonDown (0)) {
startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
void OnMouseUp () {
if (Input.GetMouseButtonUp (0)) {
endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = startPos - endPos;
myRigidbody2D.isKinematic = false;
Vector2 force = startPos - endPos;
Vector2 clampedForce = Vector2.ClampMagnitude(force, maxDrag) * Power;
myRigidbody2D.AddForce (clampedForce, ForceMode2D.Impulse);
}
}
There is an empty gameobject named battlehandler that should handle the turns but I am stuck; the script is:
public enum gameState { Start,p1Turn,p2Turn,Won,Lost}
public class battleSsystem : MonoBehaviour {
public gameState state;
int randomTurn = 0;
int p1TurnCount = 5;
int p2TurnCount = 5;
bool flag = true;
// Start is called before the first frame update
void Start()
{
state = gameState.Start;
StartCoroutine(setupBattle());
randomTurn = Random.Range(1, 1);
if (randomTurn == 1) {
state = gameState.p1Turn;
Debug.Log("P1 Turn");
p1TurnCount--;
} else if (randomTurn == 2)
{
state = gameState.p2Turn;
p2TurnCount++;
Debug.Log("P2 Turn");
}
}
private void Update() {
if (state == gameState.p1Turn) {
p1Enable();
p2Disable();
if (flag) {
p1TurnCount--;
flag = false;
}
Debug.Log("p1 has left: " + p1TurnCount);
} else if (state == gameState.p2Turn) {
p1Disable();
p2Enable();
Debug.Log("p2 has left: " + p2TurnCount);
}
if (p1TurnCount < p2TurnCount) {
state = gameState.p2Turn;
} else {
state = gameState.p1Turn;
}
}
IEnumerator setupBattle() {
yield return new WaitForSeconds(0f);
}
IEnumerator p1Turn() {
p1Enable();
p2Disable();
yield return new WaitForSeconds(10);
}
void p2Disable() {
GameObject[] objs;
objs = GameObject.FindGameObjectsWithTag("p2");
foreach (GameObject p2 in objs) {
p2.GetComponent<ballController>().enabled = false;
}
}
void p2Enable() {
GameObject[] objs;
objs = GameObject.FindGameObjectsWithTag("p2");
foreach (GameObject p2 in objs) {
p2.GetComponent<ballController>().enabled = true;
}
}
void p1Disable() {
GameObject[] objs;
objs = GameObject.FindGameObjectsWithTag("p1");
foreach (GameObject p1 in objs) {
p1.GetComponent<ballController>().enabled = false;
}
}
void p1Enable() {
GameObject[] objs;
objs = GameObject.FindGameObjectsWithTag("p1");
foreach (GameObject p1 in objs) {
p1.GetComponent<ballController>().enabled = true;
}
}
I want to select a random turn at first then change the turn after the selected player has made their turn.
How should I tackle this problem?
I am making a turn based game and I want to switch turns after a user is done back and forth. I cant use update as it will restart the turn every frame.
public enum gameState { Start,p1Turn,p2Turn,Won,Lost}
public class battleSsystem : MonoBehaviour { public gameState
state; int randomTurn = 0;
int p1TurnCount = 5;
int p2TurnCount = 5;
bool flag = true;
// Start is called before the first frame update
void Start() {
state = gameState.Start;
StartCoroutine(setupBattle());
randomTurn = Random.Range(1, 3);
if (randomTurn == 1) {
state = gameState.p1Turn;
} else if (randomTurn == 2) {
state = gameState.p2Turn;
}
if (state == gameState.p1Turn) {
Debug.Log("P1 Turn start");
p1Turn();
}
if (state == gameState.p2Turn) {
Debug.Log("P2 Turn start");
p2Turn();
}
}
IEnumerator setupBattle() {
yield return new WaitForSeconds(0f);
}
void p1Turn() {
Debug.Log("inside p1 turn");
p1Enable();//enables the player 1 controller
p2Disable();//disables player 2 controller
}
void p2Turn() {
Debug.Log("inside p2 turn");
p2Enable();//enables the player 2 controller
p1Disable();//disables player 1 controller
}