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)?
– Tarliusa "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?
– mh4simple 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
– sdgdSince 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
– Tarliuswell 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
– mh4