Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mimoali · Nov 24, 2021 at 11:44 AM · algorithmhow-to

Im creating a dr.mario style game however code its not functioning correctly

Im creating a dr.mario style game however code its not functioning correctly . so i started with creating a tetris game however im getting stuck on how to incorporate match 4 instead of delete line and how to split the game object for it to drop the leftovers till it hits something or the ground

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pill : MonoBehaviour
 {
     private float pTime;
     public float fallTime = 0.8f;
     public static int height = 20;
     public static int width = 8;
     public Vector3 rotationPoint;
     private static Transform[,] grid = new Transform[width, height];
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     void CheckForLines()
     {
         for (int i = height -1; i >= 0; i--)
         {
             if(HasLine(i))
             {
                 DeleteLine(i);
                 RowDown(i);
             }
         }
     }
 
     bool HasLine(int i)
     {
 
         for (int j = 0;  j < width; j++)
         {        
             if (grid[j, i] == null){
                 return false;
             }
             
         }
         
         return true;
 
     }
 
     void DeleteLine(int i)
     {
         for (int j = 0; j < width; j++)
         {
             Destroy(grid[j,i].gameObject);
             grid[j,i ] = null; 
         }
     }
 
     void RowDown(int i )
     {
         for(int y = i; y < height;  y++)
         {
             for (int j = 0; j < width; j++)
             {
                 if(grid[j,y] != null)
                 {
                     grid[j, y - 1] = grid[j, y];
                     grid[j, y] = null;
                     grid[j, y - i].transform.position -= new Vector3(0,1,0);
                 }
             }
         }
     }
 
     void AddToGrid()
     {
         foreach (Transform children in transform)
         {
             int roundedX = Mathf.RoundToInt(children.transform.position.x);   
             int roundedY = Mathf.RoundToInt(children.transform.position.y);  
             grid[roundedX, roundedY] = children; 
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             transform.position += new Vector3(-1, 0, 0);
 
             if(!ValidMove())
             {
                 transform.position -= new Vector3(-1, 0, 0);
             }       
         } 
 
         else if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             transform.position += new Vector3(1, 0, 0);
 
             if(!ValidMove())
             {
                 transform.position -= new Vector3(1, 0, 0);
             }        
         }
 
         else if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), 90);
             if(!ValidMove())
             {
                 transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), -90);
             } 
         }
 
         if(Time.time - pTime > (Input.GetKey(KeyCode.DownArrow) ? fallTime / 10 : fallTime))
         {
             transform.position += new Vector3(0, -1, 0);
 
             if(!ValidMove())
             {
                 transform.position -= new Vector3(0, -1, 0);
                 AddToGrid();
                 CheckForLines();
                 this.enabled = false;
                 FindObjectOfType<spawner>().NewPill();
             }        
 
             pTime = Time.time;          
         }
     }
 
     bool ValidMove()
     {
         foreach (Transform children in transform)
         {
             int roundedX = Mathf.RoundToInt(children.transform.position.x);   
             int roundedY = Mathf.RoundToInt(children.transform.position.y);  
 
             if(roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height)
             {
                 return false;
             }
             
             if(grid[roundedX, roundedY] != null)
             {
                 return false;
             }
         }
 
         return true;
     }
 }
 

alt text

screenshot-2021-11-22-230502.png (221.7 kB)
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

171 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

Related Questions

How do I highlight all available paths with Dijkstra's algorithm on a tile based map? 2 Answers

How to sort out a piano tile game algorithm ? 0 Answers

Multiple Hex placement grid 2 Answers

the dimensions on the phone and the computer look different. 0 Answers

Game Score System Help 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