- Home /
Problems destroying object on collision with Terrain
I need to destroy an object when it collides with my terrain. From what I've read in other posts I need to use OnCollisionEnter, not OnTriggerEnter and object cannot have 'is trigger' checked (and there is my problem) the object that needs to be destroyed 'has' to have a trigger as a number of other game mechanics depend on it.
How do I get around this problem
???
Answer by Digital-Phantom · Mar 12, 2015 at 06:48 AM
Ok its fair to say this problem still perplexes me, even though its now resolved.
There are many conflicting answers to this issue, most of which say Terrain Colliders are not triggers or this/that is no longer valid in Unity 5.
I stripped away all the crap from my project and basically put in the bare essentials on another new build. Created the most basic script and tried again.
using UnityEngine;
using System.Collections;
public class DetectCollision : MonoBehaviour
{
public GameObject playerExplosion;
void OnTriggerEnter (Collider other)
{
Debug.Log ("Collision Detected");
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
}
}
}
I made no adjustments to my Terrain collider, simply added this script to the terrain. My player object has a rigidbody attached, a mesh collider, kinematic is unchecked and collision detection is set to discrete.
Everything works fine now. Thought I may have some issues as the player object moves quite quickly, but nothing, its all good (at least for now)
The only issue I did have was that Unity 5 has some weird bug where sometimes the terrain collider is not in alignment with the terrain (so you either explode prematurely or move though the terrain) I believe the UT guys are looking into this so I'm guessing it will be patched soon. As a quick fix just save your project and restart unity, that seems to fix it.
Anyway, hopefully this will help any noobs (myself included there) in case they are having similar issues.
:)
Answer by AndrewToth · Mar 10, 2015 at 07:13 PM
Don`t worry u can use OnTriggerEnter aswell but the ground or the object you want to destroy needs an attached rigidbody otherwise it will not work.
Answer by hexagonius · Mar 10, 2015 at 05:15 PM
Use OnTriggerEnter with isTrigger on the Collider. It will work (just tested that myself)
Terrain Collider doesn't have an 'is trigger' option.
Are you suggesting I remove the terrain collider and use a mesh collider ?
You said
the object that needs to be destroyed 'has' to have a trigger
set it to isTrigger and have OnTriggerEnter on either it or on the tarrain (depending on who shall react)
Player object has mesh collider, rigidbody and has 'is trigger' checked (this object has to have a collider/trigger)
Terrain has standard Terrain Collider, a rigidbody (I've tried with and without rigidbody and same result)
I need the player object to be destroyed when it collides with the Terrain/Wall.
using UnityEngine;
using System.Collections;
public class CanyonWall : $$anonymous$$onoBehaviour
{
public GameObject playerExplosion;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Terrain")
{
Debug.Log("You Crashed");
Instantiate(playerExplosion, transform.position, transform.rotation);
}
Destroy (gameObject);
}
}
I have a custom tag attached to my Terrain, spelling and capitalisation are correct. I get nothing, not even the Debug message.
???
Oh and I'm using Unity 5
Wanted to test it out and got this message.
"TerrainColliders can no longer act as triggers since Unity 5.0", looks like you have to use colliders after all.
Beginning to think it might even be a glitch in Unity 5. Used all variations of OnTriggerEnter / OnCollisionEnter, different mesh types, 'is trigger' checked and not checked, with and without rigidbody.
Will start again tomorrow, maybe things clearer after some sleep...lol