- Home /
Question by
MarekNijaki · Feb 14, 2018 at 06:21 PM ·
movementcoroutinewait
Wait until movment animation ends
Hi there, i am building a chess game. In my 'Piece' class i have function 'Sel' that calculate allowed moves, select that piece (animiate movment) then return array of allowed moves. Problem is that function 'Sel' return allowed moves before movment animation is done. How to fix it (wait until movment is done, then return array)?
This is my so far code:
public abstract class Piece:MonoBehaviour
{
public abstract bool[,] MovesGet(Piece[,] pieces,bool verify);
public bool[,] Sel()
{
// Actualization of allowed moves.
bool[,] moves=this.MovesGet(BoardManager.Instance.Pieces,true);
// Actualize height of the chess piece (visualy select it).
PieceMove(this.transform.position+new Vector3(0f,1f,0f));
//debug
Debug.Log("after sel");
// Return array of allowed moves.
return moves;
} // End of Sel
// coroutine
private IEnumerator coroutine;
// Movment animation speed.
private float move_speed = 5.0f;
// Information if piece is moving (for movement animations).
private bool is_moving = false;
// Piece target position.
private Vector3 target_pos;
// Move piece to target position.
private void PieceMove(Vector3 pos)
{
// Set target position.
this.target_pos=pos;
// Set information about movment animation.
this.is_moving=true;
StartCoroutine(MovementCoroutine());
Debug.Log("PieceMove");
} // End PieceMove
// animate movment
public IEnumerator MovementCoroutine()
{
float step = this.move_speed*Time.deltaTime;
Debug.Log("x1");
while(this.is_moving)
{
Debug.Log("x2");
transform.position=Vector3.MoveTowards(transform.position,this.target_pos,step);
if(Vector3.Distance(transform.position,this.target_pos)==0)
{
this.is_moving=false;
}
yield return new WaitForSeconds(0.1f);
}
Debug.Log("x3");
}
}
Comment