- Home /
Turn Order
Edit2: So I got this to work (thank you @Anonymous700 )
public class BattleManager : MonoBehaviour {
private bool turn = true;
public BattleMove Player1;
public BattleMove Player2;
// Use this for initialization
void Start () {
Player2.ActionComplete = true;
}
// Update is called once per frame
void Update () {
if (Player1.ActionComplete == true) {
if (turn == true) {
turn =! turn;
Player2.ActionComplete = false;
Debug.Log ("Player2Go");
}
}
if (Player2.ActionComplete == true) {
if (turn == false) {
turn =! turn;
Player1.ActionComplete = false;
Debug.Log ("Player1Go");
}
}
}
}
However, that isn't the best way to work this...:(
EDIT:
Okay, @MaxGuernseyIII Here's my battle move script on both my players:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BattleMove : MonoBehaviour {
public Button Fight;
public Button Stay;
public bool CanFight;
public bool CanAttack;
public bool ActionComplete;
// Use this for initialization
void Start () {
Button button = Fight.GetComponent<Button> ();
button.onClick.AddListener (TaskOnClick);
Button button2 = Stay.GetComponent<Button> ();
button2.onClick.AddListener (TaskOnClick2);
}
void TaskOnClick(){
if (CanAttack == true) {
CanFight = true;
}
}
void TaskOnClick2(){
ActionComplete = true;
}
void OnCollisionStay2D(Collision2D col){
if (col.gameObject.tag == "Enemy") {
CanAttack = true;
if (CanFight && CanAttack) {
Destroy (col.gameObject);
CanAttack = false;
CanFight = false;
ActionComplete = true;
}
}
}
}
I'm trying to make a turn-based battle system.
So I have it when player1's turn is over, the player2's turn will begin and then it will go back to player1's turn.
Like so:
void Update () {
if (Player1.ActionComplete == true) {
Player2.ActionComplete = false; //tells play two to go
Debug.Log("Player2Go");
}
if (Player2.ActionComplete == true) {
Player1.ActionComplete = false;//tells player one to go
Debug.Log("Player1Go");
}
}
However, I can't get player1 to go after player2 went because the script keeps saying it's player2's turn as in the second line.
Sorry if this is confusing.
For this to work somewhere else in your code you need to set Player1.ActionComplete = true and the same for Player2 when their turn is complete. In which case there is no need to be setting any code in update.
We need a lot more context. If we just pasted that into our Unity environment and, as @unit_nick suggested, made sure one of our Player scripts (for which we would need to create stubs) had its ActionComplete flag set to true, we would see these if statements behaving as they appear to be intended.
Please widen the scope of the information you offer in your question.
What the logic is showing us is that if the players collide you can press a button and the other player will die. Is that how you intend for it to work? I expect it isn't.
I suggest you pseudo code what you want to happen and we can go from there.
The players are actually on the same side! I have given them both the ability to kill monsters when they bump into them(think kinda like a strategy rpg more) Of course I'll want to $$anonymous$$us health ins$$anonymous$$d of destroying the enemy but this is just a start.
Answer by Anonymous700 · Oct 13, 2017 at 04:43 AM
make a bool variable for turn
bool Turn=true;
if(Turn==true){
Turn=!Turn;// it will take you to the next player
/// write other code u want.
}
if(Turn==false){
Turn=!Turn;// again taking to 1st player
// write other code
}
problem with this code is both if statements will evaluate true.
Sorry, this isn't working right either... Edit: Hey! Now it's working!
Answer by LightWell · Oct 13, 2017 at 06:33 AM
I think you'd be a lot better off having a game manager script instead of seperately storing these variables, not only would this be a lot more flexible but it'd be a lot less of a headache in script brevity, maybe us an integer just to change the turn. something like this for the game manager:
public GameObject[] players;
public int turn = 0;
public void changeTurn() {
if(turn < players.Length) {
turn++
} else {
turn = 0;
}
}
some where in the actual player script
public int playerNumber; // To set whether it's player 1 or 2
public GameManager gM;
if(gM.turn == playerNumber - 1) { // -1 because computers count from zero.
// do player's turn actions
gM.changeTurn();
}
Hmm, for some reason, this isn't working. Both player are thinking it's their turn although I have given them different numbers...?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating an Interactable Grid for Tactics Game (C#) 1 Answer
Move object to position, perform action, move back to starting position 0 Answers
How can I pause one object's Update() until another class has performed its task? 2 Answers