- Home /
How to wait until player move to a specific position
Hello . I'm working on some kind of board game . The thing is I want to move player to a specific position when I push Roll button and I want to use transform.Translate function until I make sure that the position of player is my specific position (which will be change during the game) . I tried using transform.Translate but the result was a real disaster (Cause specific position - currentPosition var - changed with player postion and I tried to fix the problem by some simple conditional statement but nothing just happened) Instead I used transform.position but I'm not really happy with the result (try the code to understand better what I'm talking about) cause player and his enemy both move (really really fast) when I push the Roll button . I know the problem (at least a part of it is cause of transform.position) but I don't know how to fix it :( Here's the code :
using UnityEngine; using System.Collections;
public class PlayerBehaviour : MonoBehaviour {
public enum turn {
playerTurn,
AITurn
}
public GameObject player;
public GameObject enemy;
public Rect button1Position;
public string button1Text;
public turn currentTurn = turn.playerTurn;
private bool isInBoardForPlayer = false;
private bool isInBoardForEnemy = false;
public int numberForPlayer;
public int numberForEnemy;
private string labelText;
public bool doesPlayerMove = false;
public bool doesEnemyMove = false;
void Update () {
//Draw a nice string for us :)
{
if(numberForPlayer > 0)
labelText = "Number : " + numberForPlayer;
else{
labelText = "Please roll!";
}
}
//Check whose turn is it
{
if (currentTurn == turn.playerTurn) {
button1Text = "Roll!";
} else {
button1Text = "Wait til AI move";
numberForEnemy = Mathf.RoundToInt(Random.Range(1.0f,6.0f));
currentTurn = turn.playerTurn;
}
}
//Enter the player and the enemy in the game
{
if(!isInBoardForPlayer){
if(numberForPlayer == 1){
player.transform.position = new Vector3(0,.5f,0);
isInBoardForPlayer = true;
doesPlayerMove = true;
}
}
if(!isInBoardForEnemy){
if(numberForEnemy == 1){
enemy.transform.position = new Vector3(0,.5f,0);
isInBoardForEnemy = true;
doesEnemyMove = true;
}
}
}
//Move Player and Enemy
{
if(isInBoardForPlayer && !doesPlayerMove){
Vector3 currentPosition;
currentPosition = player.transform.position;
currentPosition.x += numberForPlayer;
if(currentPosition.x > 9){
currentPosition.z += 1;
currentPosition.x -= 10;
}
currentPosition.y = .5f;
player.transform.position = currentPosition;
doesPlayerMove = true;
doesEnemyMove = false;
}
else if(isInBoardForEnemy && !doesEnemyMove){
Vector3 currentPosition = enemy.transform.position;
currentPosition.x += numberForEnemy;
if(currentPosition.x > 9){
currentPosition.z += 1;
currentPosition.x -= 10;
}
currentPosition.y = .5f;
enemy.transform.position = currentPosition;
doesEnemyMove = true;
}
}
}
void OnGUI () {
if(GUI.Button(button1Position,button1Text)){
if (currentTurn == turn.playerTurn) {
numberForPlayer = Mathf.RoundToInt(Random.Range(1.0f,6.0f));
currentTurn = turn.AITurn;
if(doesPlayerMove)
doesPlayerMove = false;
doesEnemyMove = false;
}
}
GUI.Label (new Rect (550, 100, 200, 50), labelText);
}
}
English isn't my native language so sorry about any possible mistakes in vocabs,grammars,etc.
Your answer
Follow this Question
Related Questions
Stop moving at the target position by using Vector3.forward 0 Answers
Match position of two object without making one a child of the other 4 Answers
UnityEngine.Component:get_transform() Error 1 Answer
Transform.Position Collision Issue 0 Answers
Transform position changes multiplies value by 7.61 1 Answer