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 /
This question was closed Jan 20, 2017 at 03:20 AM by Jimanator for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jimanator · Jan 18, 2017 at 12:38 PM · c#integer2d array

Object fitting system (2d array) Tetris style.

hello, I am trying to make a system using a 2d array that will pick an object to put in a slot. sort of like a tetris kind of fitting thing. I cant quite figure out how that would be done. I know how to choose a random object I just need the part that checks if it can fit and then if not pick a smaller one.

Could someone help, thanks

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jan 18, 2017 at 02:02 PM

Huh? Tetris does not check if the current piece fit anywhere, it's simply random. If the player has bad luck he looses because he can't place the object anywhere useful. It's not clear what exactly you're asking here. Do you have problems picking an object at random or do you actually need help with the moving mechanics of the pieces ( i.e. can the piece move down here, can the user move it left / right without colliding somewhere).

You should ask more specific questions. I try to answer anyways...

Games like tetris are grid based (that should be clear since a 2d array is a grid). But that means each piece is made up of "grid cells":

 // the default tetris pieces
 .......  .......  .......  .......  .......  .......  .......
 .......  .......  .......  .......  .......  .......  .......
 ...O...  ...O...  ...O...  ...OO..  ..OO...  .......  ...OO..
 ...O...  ...O...  ..OOO..  ..OO...  ...OO..  ..OOOO.  ...OO..
 ..OO...  ...OO..  .......  .......  .......  .......  .......
 .......  .......  .......  .......  .......  .......  .......


Those can be rotated around the center by 90°. All you do is rotating the cells according to the objects rotation and offset each cell based on the object position to figure out where to place each cell inside the grid. If you want to move a piece to the right for example, you just calculate each cells position for the new object position after the move and see if any cells overlap. If they overlap the move is not possible.

One approach would be to actually use GameObjects for each moving piece. Define a simple array for each piece that stores the relative gid positions of each piece.

So for example the first piece above could be defined by

 (0, 1)     // #0   ..0..
 (0, 0)     // #1   ..1..
 (0.-1)     // #2   .32..
 (-1,-1)    // #3

You can use Unity's Transform.TransformPoint method to transform the local position into a global position. This would automatically account for the object rotation and the position of the object. So if this object is located at (10, 4) and rotated 90° counter clockwise you would get

     .................................
     .................................
    5.................................
     .........OOO.....................
     ...........O.....................
     .................................
     .................................
   0 .................................
     0    5    10   15   20   25   30

So the final positions of each cell are (9, 4) (10, 4) (11, 4) (11,3)

Comment
Add comment · Show 1 · 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 Bunny83 · Jan 18, 2017 at 02:09 PM 0
Share

ps: Each piece doesn't even need to be created as seperate object. It can simply be put together automatically based on it's grid cells. At least that's what tetris does ^^.

Once a piece find it's resting position (i.e. it can't move down any further) you can actually delete the object and just mark the last cell positions as "occupied" in the game area.

Of course after each "rest" of a piece you can run a complete-line-detection across your grid, remove those lines and move the lines above down.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to i set the int to hitpoints / 10 + the current int. 1 Answer

Damage script not changing enemy's health integer 1 Answer

How to make custom 2 dimensional enum array class 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