Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by beefyt123 · Aug 01, 2016 at 02:44 AM · c#listloopingcontinuous

Loop back to beginning of list in monopoly style game

Hey all,

I am making a Monopoly clone and I am having a problem with going around the board. I am using tiles and storing their positions in a List which moves the player depending on the dice roll.

My problem is when I go around the board and get to the last tile I am not sure how to loop it back to the beginning of the list so the player will continue pass go and start moving around the board again.

My current code is:

     IEnumerator movePlayer(Token currentPlayerNumber, int rollValue)
     {
         isPlayerMoving = true;
         //Get tile player is currently on
         int currentTile = currentPlayerNumber.CurrentTileID;
 
         //Move player according to dice value
         for(int i = 0; i < rollValue; i++)
         {
                 float t = 0;
 
                 //Makes the player move one tile every roll until it has reached the end position
                 Vector3 startPosition = tile[(currentTile + i)].transform.position + playerPosition[currentPlayerNumber.ID];
                 Vector3 endPosition = tile[(currentTile + i + 1)].transform.position + playerPosition[currentPlayerNumber.ID];
 
                 while (t < 1f)
                 {
                     t += Time.deltaTime * 4f;
 
                     currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
 
                     yield return null;
                 }
 
                 yield return null;
         }
 
         //Reset current tile position
         currentPlayerNumber.CurrentTileID = currentTile + rollValue;
 
         isPlayerMoving = false;
 
         StartCoroutine(tileActions());
     }

Any one have any ideas on how to achieve this?

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 ITCodeeersss · Feb 16, 2018 at 06:54 PM 0
Share

Hi @beefyt123 din you finish your work?? i also creating monopoly game with a twist do you have some sample project that i can use as a guide? thanks in advance

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by smnerat · Aug 01, 2016 at 04:49 AM

Instead of using currentTile, you can use variable for the startTile and endTile index. Then you just have to check to see if the endTile value is greater than the last index of your tile array or not. This is untested code, but it should be enough to get the idea.

 IEnumerator movePlayer(Token currentPlayerNumber, int rollValue) {

     isPlayerMoving = true;
     //Get tile player is currently on
     int currentTile = currentPlayerNumber.CurrentTileID;
     int sTile = currentTile;
     int eTile;
 
     for(int i = 0; i < rollValue; i++) {
             
         float t = 0;
         eTile++;
 
         if(eTile > tile.length)
             eTile = 0;
 
         //Makes the player move one tile every roll until it has reached the end position
         Vector3 startPosition = tile[sTile].transform.position + playerPosition[currentPlayerNumber.ID];
         Vector3 endPosition = tile[eTile].transform.position + playerPosition[currentPlayerNumber.ID];
 
         while (t < 1f) {
             t += Time.deltaTime * 4f;
             currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
             yield return null;
         }
 
         sTile = eTile;
         yield return null;
     }
     //Reset current tile position
     currentPlayerNumber.CurrentTileID = currentTile + rollValue;
     isPlayerMoving = false;
     StartCoroutine(tileActions());
 }
Comment
Add comment · Show 3 · 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 beefyt123 · Aug 01, 2016 at 05:20 AM 0
Share

Thanks for the quick reply. I will try implementing what you suggested now.

EDIT I have tried what you suggested and now my player moves around the board past go as I want but only for that current roll. As soon as either player comes to a stop on the second lap around the board then I receive the error message below.

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[monopoly.Tile].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GameController+c_Iterator2.$$anonymous$$oveNext () (at Assets/Scripts/GameController.cs:138) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(IEnumerator) c_Iterator1:$$anonymous$$oveNext() (at Assets/Scripts/GameController.cs:131)

Any thoughts?

avatar image smnerat beefyt123 · Aug 01, 2016 at 03:56 PM 0
Share

Like I mentioned, this is untested code. $$anonymous$$y first guess is that it when I wrote this, once everything is done, the players currentTile is never set to the adjusted value.

At line 31, currentPlayerNumber.CurrentTileID is set to current + roll. So when this is called again, sTile is set to current + roll, which is out of range, and only eTile is adjusted in the loop. $$anonymous$$aybe try setting currentPlayerNumber.CurrentTileID to eTile inside of the loop right after line 27. That way sTile will start out as an adjusted value.

avatar image beefyt123 smnerat · Aug 01, 2016 at 10:48 PM 0
Share

Thanks again I had a thought about what you said and I changed line 31 to currentPlayerNumber.CurrentTileID = eTile and everything seems to be working. I was able to move around the board multiple times so thanks for your help.

If anyone else is interested my final code is:

 IEnumerator movePlayer(Token currentPlayerNumber, int rollValue)
     {
         isPlayer$$anonymous$$oving = true;
         //Get tile player is currently on
         int currentTile = currentPlayerNumber.CurrentTileID;
         int sTile = currentTile;
         int eTile = currentTile;
 
         for (int i = 0; i < rollValue; i++)
         {
 
             float t = 0;
 
             eTile++;
 
             if (eTile >= tile.Count)
             {
                 eTile = 0;
                 passGo();
             }
                 
 
             //$$anonymous$$akes the player move one tile every roll until it has reached the end position
             Vector3 startPosition = tile[sTile].transform.position + playerPosition[currentPlayerNumber.ID];
             Vector3 endPosition = tile[eTile].transform.position + playerPosition[currentPlayerNumber.ID];
 
             while (t < 1f)
             {
                 t += Time.deltaTime * 4f;
                 currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
                 yield return null;
             }
 
             sTile = eTile;
             
             yield return null;
         }
 
         //Reset current tile position
         currentPlayerNumber.CurrentTileID = eTile;
         StartCoroutine(tileActions());
     }

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

214 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

Related Questions

How do you continuously rotate an object? 1 Answer

Most efficient way to store information in an inspector dropdown menu? 0 Answers

Using a specific function from a generic object. 0 Answers

check if a list contains the same id as in another list 1 Answer

Array with pushing values? 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