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 mh4 · Apr 03, 2013 at 05:42 AM · c#collisioncollider2d game

How to do collision in 2d game?

Hello guys. Attached below is a picture of what I want to do.

alt text

The question is how do I do this? Because I tried using Collision boxes with IsTrigger enabled:

 // when TB starts to touch anything
     void OnTriggerEnter(Collider Other)
     {
         Current_Collider = Other;
         
         if (Other.collider.tag == "EndPoint")
         {
             TB_State = TetrisBabyState.Dead;
             return;
         }
         
         if (Other.collider.tag != "Wall")
         {
             if (TB_State != TetrisBabyState.Walking)
             {
                 TB_State = TetrisBabyState.Walking;
             }
         }
     }
     
     // when TB stops touching anything
     void OnTriggerExit(Collider Other)
     {
         if (Other == Current_Collider)
         {
             TB_State = TetrisBabyState.Falling;
         }
     }

I also tried OnCollisionEnter:

 void OnCollisionEnter(Collision Other)
     {
         //Debug.Log("( " + Other.contacts[0].point.x + ", " + Other.contacts[0].point.y + " )");
         //Debug.Log("Hit " + Other.collider.name);
         if (Other.collider.gameObject.tag == "Player")
         {
         }
         
         Current_Collider = Other.collider;
         
         if (TB_State != TetrisBabyState.Walking)
         {
             TB_State = TetrisBabyState.Walking;
         }
         
     }
     
     void OnCollisionExit(Collision Other)
     {
         //Debug.Log("Unhit " + Other.collider.name);
         
         // if this is the latest collider
         if (Other.collider == Current_Collider)
         {
             if (TB_State == TetrisBabyState.Walking)
             {
                 TB_State = TetrisBabyState.Falling;
             }
         }
     }


and rigidbody.sweeptest too (code too messy not gonna post it here). I plan to redo this collision from the ground up so if anyone can give me a push in the right direction on how to go about doing this would be much appreciated. Thanks in advance.

EDIT: Oh i forgot to mention. If possible i do not want to use too much physics. I am moving the characters by transform. So minimal physics will be nice. Thanks :)

ask_tb2.jpg (147.9 kB)
Comment
Add comment · Show 5
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 Eric5h5 · Apr 03, 2013 at 06:27 AM 1
Share

If you're using blocks where everything is in a grid, the simplest way is not to use physics/colliders at all, but use a 2D array to represent the world. Then you can just check the appropriate array entry.

avatar image mh4 · Apr 03, 2013 at 08:43 AM 0
Share

Hi @Eric5h5, I was thinking the same thing too and i did this which works:

 public class Init : $$anonymous$$onoBehaviour {
     public Transform Cube;
     
     private int Num_Of_X_Grids;
     private int Num_Of_Y_Grids;
     
     private int Tile_Size;
     
     private int Offset_X;
     private int Offset_Y;
     
     public Object[,] GridArray;
 
     // Use this for initialization
     void Start () {
         Offset_X = -(1920/2);
         Offset_Y = -(1080/2);
         Tile_Size = 64;
         
         Num_Of_X_Grids = 1920/Tile_Size;
         Num_Of_Y_Grids = 1080/Tile_Size;
         
         GridArray = new Object[Num_Of_X_Grids, Num_Of_Y_Grids];
         
         for (int i = 0; i < Num_Of_X_Grids; i+=1)
         {
             for(int j = 0; j < Num_Of_Y_Grids; j+=1)
             {
                 GridArray[i,j] = Instantiate( Cube, 
                                               new Vector3((0.5f * Tile_Size) + (i * Tile_Size) + Offset_X, (0.5f * Tile_Size) + (j * Tile_Size) + Offset_Y, 0),
                                               Quaternion.identity);
                 
                 GridArray[i,j].name = "Tile";
                 //GridArray[i,j].renderer.enabled = false;
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     }
 }



However the screen will be fully white because of the cube. then i tried:

 public class Init : $$anonymous$$onoBehaviour {
     public Transform Cube;
     
     private int Num_Of_X_Grids;
     private int Num_Of_Y_Grids;
     
     private int Tile_Size;
     
     private int Offset_X;
     private int Offset_Y;
     
     public GameObject[,] GridArray;
 
     // Use this for initialization
     void Start () {
         Offset_X = -(1920/2);
         Offset_Y = -(1080/2);
         Tile_Size = 64;
         
         Num_Of_X_Grids = 1920/Tile_Size;
         Num_Of_Y_Grids = 1080/Tile_Size;
         
         GridArray = new GameObject[Num_Of_X_Grids, Num_Of_Y_Grids];
         
         for (int i = 0; i < Num_Of_X_Grids; i+=1)
         {
             for(int j = 0; j < Num_Of_Y_Grids; j+=1)
             {
                 GridArray[i,j] = Instantiate( Cube, 
                                               new Vector3((0.5f * Tile_Size) + (i * Tile_Size) + Offset_X, (0.5f * Tile_Size) + (j * Tile_Size) + Offset_Y, 0),
                                               Quaternion.identity);
                 
                 GridArray[i,j].name = "Tile";
                 GridArray[i,j].renderer.enabled = false;
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     }
 }

But i got an error ins$$anonymous$$d where it says "Assets/Scripts/Init.cs(32,33): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)"

avatar image Eric5h5 · Apr 03, 2013 at 08:59 AM 1
Share

Instantiate returns Object in C#, so you have to cast it appropriately, but you can't instantiate a Transform as a GameObject anyway. (It would be easier to read your code if you follow the standard Unity convention of using lowercase for variable names, and uppercase for methods and classes.) However I wasn't referring to an array of GameObjects, I was referring to an array of something like ints or bytes. The array is supposed to represent the game world, not be the game world. See here.

avatar image mh4 · Apr 03, 2013 at 05:05 PM 0
Share

Hi @Eric5h5 thanks for taking time to answer my question but I would also like to apologize if I'm not being very clear. Actually what I'm trying to do is a tile-based 2D game. I want to emulate what I did in OpenGL where I populate the screen with grids. In my C++ code I did something like this:

 // init 2d array
 theGridArray = new CGrid*[xNumOfGrid];
 
 for(int i=0; i < xNumOfGrid; i++)
 {
     theGridArray[i] = new CGrid[yNumOfGrid];
 }
 
 // init grids in 2d array
 for (int i=0; i<xNumOfGrid; i++)
 {
     for (int j=0; j<yNumOfGrid; j++)
     {
         // init the grids
         // i and j are the indexes in the array
         // xSize and ySize are the x and y sizes of the grid respectively
         // ID_WALL is the ID to show what to display and what the collision is. in this case
         // wall just means you can't walk through it
         theGridArray[i][j].Init(i, j, xSize, ySize, ID_WALL);
     }
 }

where my camera is orthogonal and pixel-based. The origin is also at the top left corner of the screen. To show you what i mean: alt text

I'm a little lost and I am wondering how to replicate it in Unity since I think Unity's camera is not pixel based and the origin is definitely in the center of the screen: alt text

Another thing is that previously the code i posted was wrong as the grid was initialised in runtime. How do i make a grid such that my artist can enter the parameters on how many grids he wants on the X and Y axis and he can also choose individual grids and assign them their materials/texture?

Really unsure on how to do this and any help will be much appreciated. :)

cam_ask_2.jpg (61.5 kB)
cam_ask_1.jpg (45.2 kB)
avatar image mh4 · Apr 03, 2013 at 05:07 PM 0
Share

Also I saw your tetrisclone and it was not what I was really looking for. Really sorry for that but thanks for helping me. Really thankful for your help:)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by shokifrend007 · Dec 17, 2020 at 12:19 PM

You need to use OnTriggerEnter2D() and OnCollisionEnter2D(). the other ones are ment for 3D.

Comment
Add comment · 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

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

11 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

Related Questions

Trouble with Physics.IgnoreCollision 0 Answers

Unity Colliders are Overlapping/Intersecting 2 Answers

Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer

If statement null check not working as expected 1 Answer

draw canvas/2D space Box Colliders on top of 3D objects that are located in world space? 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