How do I give the player a limited amount of moves? So if the player runs out of moves, a game over screen appears. My game is on a grid and only also horizontal and vertical movement. It also prevents the user from holding down to move.
{
public float moveSpeed = 5f;
public Transform movePoint;
public LayerMask whatStopsMovement;
public Animator anim;
// Start is called before the first frame update
void Start()
{
movePoint.parent = null; // lets the Transform keep its local orientation rather than its global orientation
}
// Update is called once per frame
bool isCanMove = true;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, movePoint.position) <= .05f)
{
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f && isCanMove)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
isCanMove = false;
StartCoroutine(WaitBeforeMove());
}
}
else
if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f && isCanMove)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
isCanMove = false;
StartCoroutine(WaitBeforeMove());
}
}
anim.SetBool("moving", false); // Checks whether the player is moving
}
else
{
anim.SetBool("moving", true);
}
}
IEnumerator WaitBeforeMove()
{
yield return new WaitForSeconds(1f); // Prevents user from holding down to move
isCanMove = true;
}
public void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Trigger!");
}
}