How to add limited player movement on grid?
My game is gridbased and only allows vertical and horizontal movement.
I want to be able to make it so the player is given a certain number of moves and when that hits 0, it's game over.
I was told that making a timer variable that goes up everytime the player moves and once it reaches a certain number it disables the move key. But I'm very new to unity and I can't seem to figure this out.
{ 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
count = PlayerPrefs.GetInt("score", 1);
}
// 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 (isCanMove && Input.GetButtonDown("Horizontal"))
{
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 (isCanMove && Input.GetButtonDown("Vertical"))
{
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(0.5f); // Prevents user from holding down to move
isCanMove = true;
}
public void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Trigger!");
}
}
Answer by DenisIsDenis · 2 days ago
Hello. The motion counter should be implemented like this:
public float moveSpeed = 5f;
public Transform movePoint;
public LayerMask whatStopsMovement;
public Animator anim;
int countOfMoves; // Counter of the number of moves
public int maxCountOfMoves = 10; // Maximum number of moves
// 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
countOfMoves = 0; // At the start of the level, the player's number of moves is zero
}
// 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)
{
bool isRemainMoves = countOfMoves < maxCountOfMoves; // checking and writing to a variable whether there are moves left for the level
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f && isCanMove && isRemainMoves)
{
if (isCanMove && Input.GetButtonDown("Horizontal"))
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
countOfMoves++; // Increase counter
isCanMove = false;
StartCoroutine(WaitBeforeMove());
}
}
}
else
if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f && isCanMove && isRemainMoves)
{
if (isCanMove && Input.GetButtonDown("Vertical"))
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
countOfMoves++; // Increase counter
isCanMove = false;
StartCoroutine(WaitBeforeMove());
}
}
}
anim.SetBool("moving", false); // Checks whether the player is moving
}
else
{
anim.SetBool("moving", true);
}
}
In the comments in the code, I pointed out the meaning of the lines of code I added. Just replace the Update() and Start() functions and variable declarations with the code I added.
P.S. If you think the answer solved your problem, you can accept the answer.
Your answer
Follow this Question
Related Questions
Joystick Movement Not Smooth Enough 0 Answers
Is there any way to trigger a popUp UI when a player steps in a specific set of tiles? 0 Answers
Moving moving Object with left/right arrow keys in a circular direction 3 Answers
2D android swipe movement,2D Mobile Movement 0 Answers
Rotating weapon in top-down 2D 0 Answers