Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 ecesis_llc · Aug 05, 2015 at 12:35 PM · 2dienumeratorstartcoroutinegridmove

2D Grid Movement - StartCoroutine, IEnumerator, SmoothMovement best practices?

Unity: 5 Type: 2D Axes: x,y

My question, I think, is dealing with StartCoroutine and moving on a grid. The roguelike tutorial seems like a good example, but as soon I setup my own grid size behavior becomes different. I also have a question related to the roguelike tutorial and how it appears that StartCoroutine is called twice to move.

[Edit] Movement is turn based. Player moves, then enemies are cycled for movement.

Roguelike Tutorial - Calls StartCoroutine Twice?

The tutorial has a method AttemptMove. The flow to StartCoroutine is AttemptMove > Move > StartCoroutine(SmoothMovement(end)). After calling that method it then calls move again. Would this cause strange behavior on the moving GameObject?

 protected override void AttemptMove <T> (int xDir, int yDir)
     {
         food--;
         foodText.text = "Food: " + food;
 
         //This results in a StartCoroutine by calling Move
         base.AttemptMove <T> (xDir,yDir);
 
         RaycastHit2D hit;
 
         //This also results in a StartCoroutine
         if (Move (xDir, yDir, out hit))
         {
             SoundManager.instance.RandomizeSfx(moveSound1,moveSound2);
         }
 
         CheckIfGameOver();
 
         GameManager.instance.playersTurn = false;
     }


Best Practice?

My issue with trying to snap a moving unit to the grid appears to have been fixed by preventing any more user input until the StartCoroutine has finished. I added a isMoving bool to the class that handles movement. If the object is already moving new input is ignored.

My GameObject has a Rigidbody2D and is is kinematic (basically pulled from the tutorial). If I do not prevent user input during that coroutine my object moves off the grid fairly quickly and begins to slop around as input appears to be running across multiple coroutines. I take it that the movement coroutine is starting with a position off the grid during the movement animation.

Is a flag to prevent further input the correct way to address that behavior or is that just a hack to work around something I don't understand in Unity. I am new to Unity and C#.

GridMove is another 2D grid movement example. It also uses a flag to prevent further input until the coroutine is finished.

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

2 People are following this question.

avatar image avatar image

Related Questions

Collision with grid based movement 0 Answers

making a 2D character Speak for different lenghts of time 2 Answers

how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers

Coroutines IEnumerator not working as expected 2 Answers

2D Animation does not start 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