Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SlyyGuyy · Mar 31, 2014 at 10:30 PM · c#movementgravityblocksmatching

Shifting blocks to the right to remove empty spaces - C#

In the block matching game that I am creating I currently have the blocks falling upon elimination and because of this, there's a lot of leftover blocks and some empty spaces that could be filled with blocks and cause more matches to be available. How can I get the game to shift specific blocks over to the right in order to fill those spaces? I'll post the code I have currently that drops them down, now I just need them to shift to the right. I've also attached a screenshot of what's happening in the grid so that you can see the openings that the game is creating

 public void applyGravity() {
             // Go through every column
             for (int y = 0; y < 10; y++) {
                 // Track empty positions in the colum
                 int lastEmptyPos = 0; 
                     // Go through every row, starting from the bottom
                         for (int x = 0; x < 10; x++) {
                         // Check to see that the current block is not empty
                         if (grid[x, y].renderer.material.mainTexture != null) {
                                // Check for gaps
                             if (lastEmptyPos < x) {
                                 // Drop the blocks down
                                 grid[lastEmptyPos, y].renderer.material.mainTexture = grid[x, y].renderer.material.mainTexture;
                                 grid[x, y].renderer.material.mainTexture = null;
                                }
                     // Since we have a block (regardless if we moved it), the tower must be +1 higher
                     lastEmptyPos++;
             }
         }
     }
 }

alt text

screen shot 2014-03-31 at 5.28.58 pm.png (26.3 kB)
Comment
Add comment · Show 2
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 getyour411 · Mar 31, 2014 at 11:01 PM 0
Share

Clarify please - you want all blocks in all rows to move to the right if there's white space/empty to the right? Example say the red block in col 3, row0 should shift to the right? Should it then shift again because there are two white spaces? When it moves, the green block will now have white space to its right, should it shift? etc

avatar image SlyyGuyy · Mar 31, 2014 at 11:50 PM 0
Share

What you described, yes, that is basically what I want. I want my loop to go through all the columns and rows and see if there's emptiness on the right and just "push" everything over to the right. So, for example, (from the right) the 1 red piece and 2 blue blocks would shift over to the right, etc.

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

20 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Round Planets and Movement on them 2 Answers

Change Block Position In Block Matching Game C# 2 Answers

Multiple Cars not working 1 Answer

How to destroy blocks that are matching in color C# 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