How do we make it so you can't hold down to move?
A solution I found was making a bool that needs to be true whenever you move and every time you move start a co-routine setting that variable to false, then using WaitForSeconds() and put in the time you wan't to have between inputs, and then set it to true. But Im not sure how to implement.
public class PlayerController : MonoBehaviour
{ 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
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)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
} else
if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement))
{
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
// Checks whether the player is moving
anim.SetBool("moving", false);
} else
{
anim.SetBool("moving", true);
}
}
public void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Trigger!");
}
}
Answer by DenisIsDenis · 4 days ago
Your idea should work. Modify your code like this:
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());
}
}
// Checks whether the player is moving
anim.SetBool("moving", false);
}
else
{
anim.SetBool("moving", true);
}
}
IEnumerator WaitBeforeMove()
{
yield return new WaitForSeconds(2.0f); // set your time
isCanMove = true;
}
I added an IEnumerator
, a boolean isCanMove
and changed the Update()
function (there is a coroutine starts and input limited).
Thanks, this helps a lot.
But I can still hold down the movement keys, I was hoping for the user to have to keep pressing and remove the option of holding down all together.
So you can add if (… && isCanMove && Input.GetButtonDown("Horizontal"))
to the first condition and if (… && isCanMove && Input.GetButtonDown("Vertical"))
to the second condition. This needs to be written where I added isCanMove earlier.
This code should work:
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f && 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 && 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());
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613061603im_/https://answers.unity.com/themes/thub/images/avi.jpg)