• 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 Dinosaurous · Mar 20, 2019 at 06:51 AM · while-loop

Stuck in a coroutine while loop

I am trying to make a dice game where when I click a button, a number between 1 to 6 is generated. The player will be moved (using IEnumerator) according to the number rolled.


The board is a square with a trigger collider above it for the player to check if it is movable in that direction. When the player reached the corner of the board, it will rotate 90 degree and is supposed to continue on to another direction, but the player will only move 1 block after it is rotated and the IEnumerator will be stuck in a while loop even though the player's transform.position is the same as endPos.

Any form of help will be appreciated.


 protected IEnumerator Move(int zDir)
     {
         moving = true;
 
         RaycastHit hit;
         Physics.Raycast(transform.position, transform.forward, out hit, 1.0f);
         
         if (hit.collider == null)
         {
             transform.Rotate(0f, 90f, 0f);
             Physics.Raycast(transform.position, transform.forward, out hit, 1.0f);
             Debug.Log("Rotated");
             yield return null;
         }
 
         Vector3 startPos = transform.position;
         Vector3 endPos = startPos + transform.TransformDirection(0f, 0f, zDir);
         Debug.Log(endPos);
 
         if (hit.collider != null)
         {
             float sqrRemainingDistance = (transform.position - endPos).sqrMagnitude;
             
             while(sqrRemainingDistance > float.Epsilon)
             {
                 Debug.Log("Moving");
 
                 Vector3 newPostion = Vector3.MoveTowards(rb.position, endPos, moveTime * Time.deltaTime);
                 
                 rb.MovePosition (newPostion);
                 
                 sqrRemainingDistance = (transform.position - endPos).sqrMagnitude;
                 
                 yield return null;
             }
             gameManager.num--;
             moving = false;
         }
 
 void Update()
     {
         Debug.DrawRay(transform.position, transform.forward);
 
         if (gameManager.num > 0 && !moving)
         {
             StartCoroutine(Move(1));
         }
     }


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 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

Related Questions

What is wrong in my code - Unity freezes 1 Answer

start coroutine everytime 2 Answers

Moving out of collision with trigger using while loop crashes Unity. 1 Answer

Can you not use while loops in unity? if so how can i change this to work,Can you not use while loops in unity? 2 Answers

While Loop crashing Unity consistently 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges