• 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 Zarquon1 · Apr 14, 2020 at 02:31 PM · game development

Why are my prefabs going through each other

As my question states I am having trouble with my Unity project, I am trying to make a tetris clone for college and my prefabs are just going though each other as if they don't have a collision box. This is the code for my tetris cubes:

{ public Vector3 rotationPoint; private float previousTime; public float fallTime = 0.8f; public static int height = 50; public static int width = 25; private static Transform[,] grid = new Transform[width, height];

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
    
     if (Input.GetKey(KeyCode.A))
     {
         transform.position += new Vector3(-1, 0, 0);
         if (!ValidMove())
             transform.position -= new Vector3(-1, 0, 0);
     }
     else if (Input.GetKey(KeyCode.D))
     {
         transform.position += new Vector3(1, 0, 0);
         if (!ValidMove())
             transform.position -= new Vector3(1, 0, 0);
     }
     else if (Input.GetKeyDown(KeyCode.W))
     {
         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 - previousTime > (Input.GetKey(KeyCode.S) ? 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<SpawnerScript>().SpawnNext();
         }
        
         previousTime = Time.time;
         
     }


 }

 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 - 1].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;
     }
 }

 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;
 }


}

and this is a link to the video of my problem https://www.youtube.com/watch?v=vG-Qo7-EekE

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

139 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

Related Questions

Game Development Blogs 2 Answers

Animating y only on an object resembling a bomb 0 Answers

CharacterController (Unity) not jumping near objects 0 Answers

Simple game jittering horribly on MacBook Pro. 1 Answer

Inverse Kinematic is not on position 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