- Home /
How to do collision in 2d game?
Hello guys. Attached below is a picture of what I want to do.
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 :)
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.
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?)"
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.
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:
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:
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. :)
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:)
Answer by shokifrend007 · Dec 17, 2020 at 12:19 PM
You need to use OnTriggerEnter2D() and OnCollisionEnter2D(). the other ones are ment for 3D.
Your answer
Follow this Question
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