- Home /
2D Collision using tags not working
Hi guys I'm having a slight problem with my 2d collision detection and I cant seem to find the problem. Basically what i want to do is make a square (movable) collide with another square (static). Both squares do not have rigidbody now (only box colliders) and the "IsTrigger" is checked. They also have the same depth(-1) and the static square has a "Unwalkable" tag (i set it using the inspector). This is my code.
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Unwalkable")
{
print ("Hit yo");
this.transform.Translate(0,0,0);
}
}
It doesnt even print "Hit yo" in the console. I tried several other methods such as
void OnCollisionEnter(Collision collision) {
if( collision.collider.gameObject.tag == "Unwalkable" )
{
print ("Hit yo");
}
}
and also
void OnTriggerEnter(Collider other)
{
//Destroy(other.gameObject);
if (other.gameObject.CompareTag("Unwalkable"))
{
print ("Hit yo");
}
}
I also tried giving both of the squares rigidbody (and unchecking "Use Gravity" and check "Is Kinematic") but nothing is working at the moment. Can anyone help me out on this one? Thanks in advance. :)
Did you try making the objects' colliders have a "3D" collider (as opposed to a "flat" one)?
a "3D" Collider? What do you mean? Im afraid i dont have any idea so most probably no I have not tried it. So how do i make a "3D" Collider?
simple you put a cube in to your scene and there you have it 3D colider with mesh renderer
btw how did you get 2D collider plane?
and you'll have to figure out that you are working in 3D engine so whatever you'll make even if 2D game it'll be 3D game in the end but looked 2D
except OnGUI
Since you said 2D objects, I'm curious if you made a 2D box collider too (ie- you set one of its dimensions to 0). This might cause problems because of how floating points have rounding errors is all
well this is how i do the cube.
1) Create a cube. Set size to 64 by 64 ("scale" in the "transform" tab in the inspector) and position is set to (0,0,-1)
2) Create a empty gameobject. Drag the cube to this empty game object. Now this gameobject is the "parent".
3) I attach the script to this gameobject.
So i guess i just check the "Box Collider" and the "IsTrigger".
Am i doing this right? Lol
Answer by Ashkan_gc · Mar 19, 2013 at 09:03 AM
Well you should write code for OnTriggerXXX where XXX is Enter/Stay/Exit and at least one of the cubes should have a rigidbody which can be isKinematic. Read the collision related pages of manual another time. The OnCollisionXXX methods only are for none kinematic physics based rigidbodies.
So.....I must add a rigidbody component to the cube? But i already did that and it didnt work :(
how, could you please let us know. i'm having the same problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094702im_/https://answers.unity.com/themes/thub/images/avi.jpg)