Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 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 markkmlam · Oct 29, 2021 at 04:06 AM · mobilecoroutinejoystickgrid based game

Coroutine called more than once

Hello all and good day!

I am new to unity and I am creating a game based on grid system. I want to move the player's unit from one grid to another based on their input from joystick. However, I am getting strange behavior of:

- unit moving less than it should be (if player move joystick four times there should be four moves)
- sometimes a movement is much slower than it should be (given the speed I set)
- from the log I see that sometimes it only record one joystick input but multiple times of movement.

I am suspecting somewhere in the code I let the coroutine to call more than once, which is not what I intend. I understand that there are similar questions that have been asked but I still couldn't get a clue on what is wrong with my code. So, I would like to obtain some advice from you guys on which direction should I check. Appreciate your time reading this and sorry for my bad English.

Thank you and have a great day!

Here are extract of my code:

 public class GameHandler : MonoBehaviour
 {
     //To get player input from joystick
     void Update()
     {
         float verticalDirection = variableJoystick.Vertical;
         float horizontalDirection = variableJoystick.Horizontal;
         if (!inAction)
         {
             inAction = true;
 
             // Joystick Up
             if (verticalDirection > 0.5 && Mathf.Abs(horizontalDirection) < 0.5)
             {
                 StartCoroutine(PlayerMove(PlayerUnit.MoveDirection.Up));
                 Debug.Log("joystick moved");
             }
 
             // Joystick Down
             else if (verticalDirection < -0.5 && Mathf.Abs(horizontalDirection) < 0.5)
             {
                 StartCoroutine(PlayerMove(PlayerUnit.MoveDirection.Down));
                 Debug.Log("joystick moved");
             }
 
             // Joystick Right
             else if (horizontalDirection > 0.5 && Mathf.Abs(verticalDirection) < 0.5)
             {
                 StartCoroutine(PlayerMove(PlayerUnit.MoveDirection.Right));
                 Debug.Log("joystick moved");
             }
 
             // Joystick Left
             else if (horizontalDirection < -0.5 && Mathf.Abs(verticalDirection) < 0.5)
             {
                 StartCoroutine(PlayerMove(PlayerUnit.MoveDirection.Left));
                 Debug.Log("joystick moved");
             }
 
             else
             {
                 inAction = false;
                 //Debug.Log("joystick moved a little");
             }
         }
     }
 
     //Player's move
     IEnumerator PlayerMove(BattleUnit.MoveDirection moveDirection)
     {
         Debug.Log("Player Move");
 
         battleUnit.UnitMove(moveDirection);
 
         while (canProceed == false)
         {
             yield return null;
         }
 
         if (activePlayerUnit.actionPoint <= 0)
         {
             EndThisTurn();
         }
 
         inAction = false; //Completed action
         canProceed = false; // Set up for next action
     }
 }
 
 
 public class BattleUnit : MonoBehaviour
 {
     // Find movement destination
     public void UnitMove(MoveDirection moveDirection)
     {
         Map map = GameObject.Find("GridMapManager").GetComponent<Map>();
         BattleGrid targetGrid = null;
 
         switch (moveDirection)
         {
             case MoveDirection.Up:
                 targetGrid = map.GetGridObject(belongGrid.GetX(), belongGrid.GetY() + 1);
                 //belongGrid.GetX
                 break;
             case MoveDirection.Down:
                 targetGrid = map.GetGridObject(belongGrid.GetX(), belongGrid.GetY() - 1);
                 break;
             case MoveDirection.Left:
                 targetGrid = map.GetGridObject(belongGrid.GetX() - 1, belongGrid.GetY());
                 break;
             case MoveDirection.Right:
                 targetGrid = map.GetGridObject(belongGrid.GetX() + 1, belongGrid.GetY());
                 break;
         }
 
         StartCoroutine(UnitMoveCoroutine(targetGrid));
     }
 
     // Move player's unit to destination
     IEnumerator UnitMoveCoroutine(BattleGrid targetGrid)
     {
         GameHandler gameHandler = GameObject.Find("GameHandler").GetComponent<GameHandler>();
         Map map = GameObject.Find("GridMapManager").GetComponent<Map>();
 
         float speed = 2.8f;
         float step = speed * Time.deltaTime;
         moveDone = false;
 
         if (targetGrid == null || targetGrid.isOccupied == true)
         {
             Debug.Log("cannot move to target");
             moveDone = true;
         }
         else
         {
             while (moveDone == false)
             {
                 transform.position = Vector3.MoveTowards(transform.position, map.GetWorldPosition(targetGrid), step);
 
                 if (transform.position == map.GetWorldPosition(targetGrid))
                 {
                     Debug.Log("Unit Arrived!");
                     actionPoint -= targetGrid.actionPointUse;
                     gameHandler.canProceed = true; // Indicate player action finished and can proceed to next action
                     moveDone = true;
                 }
                 yield return null;
             }
         }
     }
 }







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

0 Replies

· Add your reply
  • Sort: 

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

190 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

Related Questions

Mobile joystick moving my player like the game is played in portait screen, but the game is in landscape mode. 1 Answer

Making one directional virtual joystick OR smoothly transitioning between buttons 0 Answers

how to rotate object with joystick (Mobile) 0 Answers

Character Controller Initial Problem With Mobile 0 Answers

Mobile joystick is not working in unity 2018.1 only jump button works 0 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