- Home /
How to collide with everything but trigger script whenever certain object hits
I have a cannon and a wall, i have a setup cannon which shoots canonball at a wall and i have a simple script that works by deleting old wall and replacing it with new fractured one. But OnCollisionEnter doesn't work very good, because wall(or cubes) very often collides with ground or each other i tried ignoring ground but then cube falls through ground. Is there any other way? Maybe to still collide with everything but execute script whenever cannonball hits wall, but i'm not sure how to do that... PS: I tried with tags or layers but that didn't work either.
public class Destructible : MonoBehaviour {
RaycastHit hit;
public GameObject destroyedVersion;
void Start() {
}
void OnCollisionEnter(Collision collider)
{
if (hit.collider.gameObject.tag == "cannonball") {
Instantiate (destroyedVersion, transform.position, transform.rotation);
Destroy (gameObject);
}
}
}
Have you considered using a trigger collider ins$$anonymous$$d?
@RobAnthem Well, i fixed it partially, now it doesn't collide with ground but collides with player, basically what i wanted is to only collide with my cannonball, i'm now using OnTriggerEnter, which is the same but still not great because i'm failing to see a way to avoid executing my destructible script when cubes hit each other. I can't really explain what i want.. Basically i wan't my cube to execute destructible script when it's hit by something(in this way its trigger collider)Cube on top should fall as well(by adding mass so it's not flying away when bottom one explodes) player can go trough cube, but when does he doesn't execute destructible script (only cannonball does) and if a cube is falling down so when it hits the floor, script is executed. And so on.. i really can't find a way to do this.
Answer by toddisarockstar · Apr 02, 2017 at 05:21 PM
what is the raycast variable for?
if you want a collision you dont need a raycast variable. it should just look like this:
public class Destructible : MonoBehaviour {
public GameObject destroyedVersion;
void Start() {
}
void OnCollisionEnter(Collision collider)
{
if (collider.gameObject.tag == "cannonball") {
Instantiate (destroyedVersion, transform.position, transform.rotation);
Destroy (gameObject);
}
}
}
@toddisarockstar I was trying something with raycast, but then i forgot to delete it.. And that in void start i forgot to delete, i was trying a couple things, but they didn't work.
the raycast varible was also in the collision function. if what i wrote isnt working try putting a print statement in the collision function to see if thats the problem.
did you put rigidbodys and colliders on everything?
@toddisarockstar I already tried that what you wrote, as i said in comment with Rob Anthem i can't find a way how to do this. Yes i did put rigidbodys on everything, i added another box collider on my cube(i shifted it so i doesn't touch ground) i changed my script a bit and now it works, but i still doesn't work now a player can trigger or other cube( so now cubes can't be stacked) or anything else... This is my script now:
public class Destructible : $$anonymous$$onoBehaviour {
public GameObject destroyedVersion;
void Start() {
}
void OnTriggerEnter()
{
Instantiate (destroyedVersion, transform.position, transform.rotation);
Destroy (gameObject);
}
}
Very simple as you can see.