Hi everyone, I’m developing a game in which I have to add a Collider2D to destroy some gameObjects in the scene. The Collider works, but the main problem is that as gameObjects collide with the Collider they “clash” with it and so it seems they clash with a sort of wall. All I want is to destroy my gameObjects, but without notice the collision. Do you have any idea ? Thank you very much.
your need the collider because you need it to be hit by a raycast?
make it a trigger if you do not wish collisions
other solution would be to assign layers and setup in
Edit->peoject settings-> physics2d whoch layer interacts with each other
Yes I tried with Trigger, but i can’t use it because the objects I want to destroy are some instantiated spheres, and some of them are instantiated very near at the same time, and so with Trigger they overlap each other.
For my Missile Command game I used triggers. I did something like this:
void OnTriggerEnter2D (Collider2D other) {
//Do something like Destroy(other.gameObject); or Destroy(this.gameObject);
}
It works quite well and I think it is what you are trying to do.
EDIT: Ok, for your sphere problem you might want to tag your object as Sphere
and do a condition inside the trigger to ignore it if the other object is tagged as sphere.
Thank you for your replies, but as I said before i can’t use isTrigger function
Do you want the collision of two sphere object to always be ignored or do you want to have some kind of period of invincibility when it is instantiated?
No, I don’t want them to be ignored. All I want is destroy them at the collision with the collider, but without notice the collision, as it happens with isTrigger function enabled. The only problem is that i can’t use isTrigger, and so I have to find a way to make the same thing but without isTrigger function enabled
sorry if i seem a bit slow.
so your spheres cannot be triggers, that does not say the object that is the condition for the destruction cannot be a trigger
lets say are supposed to be destroyed if they crash into a wall - so the wall can be a trigger actually
Yeah, so I tried to add a script to my instantiated ball to detect the collision with a trigger object (my collider) like this, but it doesn’t work.
void OnTrigger2DEnter(){
Destroy(this);
}
I think the script should be added to the wall since it is the object that is a trigger.
//This code is executed on the Wall object, which is a trigger.
void OnTriggerEnter2D (Collider2D other) {
//other is the Ball instance that is entering the wall.
Destroy(other.gameObject);
}
void OnTriggerEnter2D(Collider2D _col)
{
if(_col.GetComponent<yourSphereScriptname>() != null)
{
Destroy(_col.gameObject);
}
}
edit: guess someone was faster
Sashenka, if you add that script to the collider, it will detect Trigger object that collide with it and destroy them…
vothka, i tried yours, but it seems not to detect any collision…
Isn’t it what you want? If a ball collide with another object, destroy the ball? If you want only certain types of object to be destroyed by the collision, tag them and do a check.
void OnTriggerEnter2D (Collider2D other) {
//Destroy the other object only if it is tagged as a Sphere, otherwise nothing happen.
if (other.gameObject.tag == "Sphere") {
Destroy(other.gameObject);
}
}
Yes now it’s ok, thank you very much !