- Home /
Having difficulties making a block be a death trigger for a ball.
I am working on a pretty simplistic game right now, and this is really impeding my ability to finish this project. I am trying to get a block to be a trigger so when the ball hits the block, the ball get's destroyed and respawns. I have tried probably every tutorial on YouTube, no scripts worked. Can you make a tag that you can put on an item that makes it a death trigger?
If you need more information, I will provide it.
Thank you.
void OnTriggerEnter(Collider col){
Destroy(col.gameObject);
}
void OnTriggerEnter(){
Destroy(gameObject);
}
Hello,
Have you marked the Collider component of the ball as "is Trigger" in the inspector ? If you want the ball to be a collider and not Trigger then you should change the code to
void OnCollisionEnter(Collision col)
Also there is no need to use two OnTriggerEnter functions. Just use
void OnTriggerEnter(Collider col)
{
Destroy (col.gameObject);
}
If the ball is marked as trigger use OnTrigger else use OnCollision. Also you have not checked if the gameobject colliding is a ball or not. the above code will destroy any gameobject co$$anonymous$$g in contact with the block. Hope this helps :)
Answer by sadowlight123 · Apr 25, 2017 at 05:11 AM
hello there,
you need to do the following: 1- this code should be on the death box if it is not
void OnTriggerEnter(Collider col){
Destroy(col.gameObject);
}
2-add a collider to both the death box and the player (if they dont have it already) 3-tick the trigger box that is on the death box
additional : add a tag to the player and call it "Player" then change the code to be like :
void OnTriggerEnter(Collider col){
if(col.tag=="Player")
Destroy(col.gameObject);
}
Hope it helps
Answer by KrisDevelopment · Apr 25, 2017 at 04:51 PM
@MiloRoban Here's the 4 things that need to be in place in order for your code to work: 1: have your code (as in the examples above) on the death trigger :
void OnTriggerEnter(Collider col){
Destroy(col.gameObject);
}
2: Add a collider to the death trigger and check the check box for trigger. Also add a collider to your ball.
3: The one thing that everyone else missed is that YOU NEED TO HAVE Rigidbody component on your ball object. Without one it wouldn't work.
Bonus: Make sure their layers are seeing eachother in the Edit>Project Settings>Physics.
Good luck!
as $$anonymous$$risDevelopment said , we did forget to tell you to add a rigidbody on at least one of the two colliders , in our case the player ball , but since your ball already moves I assume you already have it on .