Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by MrCael · Dec 21, 2020 at 10:11 AM · keyboard inputmovements

How do you suspend key presses if a bool value is true?

I am trying to make some grid based movement where the character moves one tile for every key press with this code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     private Vector3 targetPos;
     private float tileLength = 1.1f;
     private float speed = 5f;
     private bool isMoving;
     Rigidbody2D rb;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!isMoving)
         {
             isMoving = true;
 
             if (Input.GetKeyDown(KeyCode.UpArrow))
             {
                 targetPos = new Vector3(transform.position.x, transform.position.y + tileLength, 0);
                 StartCoroutine(MovePlayer(targetPos, "up"));
             } else if (Input.GetKeyDown(KeyCode.DownArrow))
             {
                 targetPos = new Vector3(transform.position.x, transform.position.y - tileLength, 0);
                 StartCoroutine(MovePlayer(targetPos, "down"));
             } else if (Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 targetPos = new Vector3(transform.position.x - tileLength, transform.position.y, 0);
                 StartCoroutine(MovePlayer(targetPos, "left"));
             } else if (Input.GetKeyDown(KeyCode.RightArrow))
             {
                 targetPos = new Vector3(transform.position.x + tileLength, transform.position.y, 0);
                 Debug.Log(isMoving);
                 StartCoroutine(MovePlayer(targetPos, "right"));
             }
 
             isMoving = false;
         }
     }
 
     private IEnumerator MovePlayer(Vector3 tPos, string direc)
     {
         switch (direc)
         {
             case "up":
                 while (transform.position.y < tPos.y)
                 {
                     rb.velocity = new Vector3(0, speed, 0);
                     yield return null;
                 }
 
                 break;
             case "down":
                 while (transform.position.y > tPos.y)
                 {
                     rb.velocity = new Vector3(0, -speed, 0);
                     yield return null;
                 }
 
                 break;
             case "left":
                 while (transform.position.x > tPos.x)
                 {
                     rb.velocity = new Vector3(-speed, 0, 0);
                     yield return null;
                 }
 
                 break;
             case "right":
                 while (transform.position.x < tPos.x)
                 {
                     rb.velocity = new Vector3(speed, 0, 0);
                     yield return null;
                 }
 
                 break;
         }
 
         rb.velocity = new Vector3(0, 0, 0);
         transform.position = tPos;
     }
 }

After a bit of testing I think what's happening is that the isMoving bool isn't getting set to true at the beginning of the loop if it was previously set to false. So basically it's just constantly set to false when I don't think it should be. It feels like there's some super simple solution to this problem that I just don't know, but I don't know where to look XD.
Any and all help is appreciated!!!

Comment
Add comment · Show 1
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 Jaxcap · Dec 21, 2020 at 10:55 AM 0
Share

GetKeyDown only triggers once per keypress anyway, so wouldn't it already have the behavior you want? What does your code do if you don't include the is$$anonymous$$oving logic?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Dec 21, 2020 at 11:56 AM

ismoving is getting set to true but you are setting it back to false before finishing the update for some reason, which makes no sense, you need to move the

              isMoving = false;

inside the coroutine and remove from the update

  private IEnumerator MovePlayer(Vector3 tPos, string direc)
      {
          switch (direc)
          {
              case "up":
                  while (transform.position.y < tPos.y)
                  {
                      rb.velocity = new Vector3(0, speed, 0);
                      yield return null;
                  }
  
                  break;
              case "down":
                  while (transform.position.y > tPos.y)
                  {
                      rb.velocity = new Vector3(0, -speed, 0);
                      yield return null;
                  }
  
                  break;
              case "left":
                  while (transform.position.x > tPos.x)
                  {
                      rb.velocity = new Vector3(-speed, 0, 0);
                      yield return null;
                  }
  
                  break;
              case "right":
                  while (transform.position.x < tPos.x)
                  {
                      rb.velocity = new Vector3(speed, 0, 0);
                      yield return null;
                  }
  
                  break;
          }
  
          rb.velocity = new Vector3(0, 0, 0);
          transform.position = tPos;
          isMoving = false;
      }
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

111 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

Related Questions

Stopping moving object going through a wall 4 Answers

How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer

i am working on an app for iOS Platform. i have a Input Field , but every time the White Classic keyboard comes out to take input. I want to display the keyboard with a black semi-transparent background. Can anyone help me out with this? 0 Answers

Get Input value? 1 Answer

How to unlink Arrow Keys Input with Keypad 2468 buttons 2 Answers


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