• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_67380 · 2 days ago · movement2d gameplayer

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!");
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DenisIsDenis · 2 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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_67380 · yesterday 0
Share

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.

avatar image DenisIsDenis unity_67380 · yesterday 0
Share

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.

avatar image unity_67380 · yesterday 0
Share

What are the ... from if (… && supposed to be? Sorry.

avatar image DenisIsDenis unity_67380 · yesterday 1
Share

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());
              }
          }
avatar image
0

Answer by unity_67380 · 23 hours ago

Absolute legend.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

307 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My player won't move, does anyone know why? 0 Answers

Character continues moving when locked onto an enemy 1 Answer

How to move a game object to a position after selecting it 0 Answers

Cardboard and Navmesh: Problem with Player Movement 0 Answers

Player not moving in the right direction instantly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges