- Home /
OnTriggerEnter not always triggering
For some reason when I fire a projectile (which has Trigger enabled) at my terrain (which is not a trigger) OnTriggerEnter is not being called every time the object hits it.
For example: https://www.dropbox.com/s/35pv4tyjvfc1svj/2015-03-17-2355-20.flv?dl=0
And here is the OnTriggerEnter function (located in the projectile script):
void OnTriggerEnter(Collider other)
{
Debug.Log("enter " + other.gameObject.name);
if (type == ProjectileType.Player && other.gameObject.tag == "Player")
return;
if (type == ProjectileType.NPC && other.gameObject.tag == "NPC")
return;
if (!exploded)
{
Debug.Log("explode " + other.gameObject.name);
exploded = true;
TerrainController.Instance.GenerateExplosion(rigidBody.position, explosionPower);
GameObject audio = (GameObject)Instantiate(audioPrefab, rigidBody.position, Quaternion.identity);
GameObject particalObj = (GameObject)Instantiate(partical, rigidBody.position, Quaternion.identity);
audio.GetComponent<AudioSource>().Play();
Destroy(particalObj, 5);
Destroy(audio, 5);
Destroy(gameObject);
if (type == ProjectileType.Player && other.gameObject.tag == "NPC")
other.GetComponent<NPCControll>().Break();
}
}
Just add rigidbody to the projectile it willl solve your problem
then your terrain must be having mesh collider just check convex via inspector in the mesh collider and retry hope it will help
http://docs.unity3d.com/ScriptReference/$$anonymous$$eshCollider-convex.html
The mesh collider for the terrain does not have "IsTrigger" Selected. Only the projectile does.
I mean you dont have to select the trigger of mesh just check the convex terrain -> mesh collider -> convex set true . in inspector
That would make the collider not match the actual terrain because it would be convex, and thus a different shape.
Answer by Subhajit-Nath · Mar 19, 2015 at 04:29 AM
There are two possibilities I can think of right now.
You are missing an active rigidbody in the projectile.
The velocity of the projectile is too high for collision detection. You will need to change the collision detection mode in the rigidbody to continuous and make the triggers and colliders much more bigger.
It already had a rigidbody, and setting the collision detection to continuous helped a little but didn't solve it completely. It's also strange, because When I shoot a projectile that goes right through, and then shoot more without moving the mouse, they also go right through. As soon as I move the mouse just a little (almost as if there are holes in the collider, however none show up when I disabled the mesh renderer to view the collider mesh), it interacts with the terrain again.
Nice one, I got a similar problem and pretty sure its due to my objects velocity being too high. Trying to find a way around it without lowering the velocity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GameObject tag to if condition 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
OnTriggerEnter On seperate object (than the script is attached to) 1 Answer