- Home /
OnTriggerEnter is often not working
I'm trying to make a shooting system. I use rigidbodys for the bullets and I want to make the hit detection with the a hitbox of the ridgidbody object. I'm using the "OnThriggerEnter"-function for that. But for some reason, the function is working sometimes and sometimes not. Sometimes the script does what it should do and sometimes, my bullet is just flying thought the object. That's my script:
Script on Objects:
import UnityEngine
class DamageTaker (MonoBehaviour):
health as int = 300
def Update ():
if health <= 0:
self.die()
def ApplyDamage(dmg as int):
health -= dmg
def die():
#Sterbeanimation
Destroy(gameObject)
Script on bullet object:
import UnityEngine
class BulletDamageScript (MonoBehaviour):
dmg as int = 50
def OnTriggerEnter (hitObj as Collider):
hitObj.SendMessage("ApplyDamage", dmg, SendMessageOptions.DontRequireReceiver)
Destroy(gameObject)
As you can see, the language is Boo.
Answer by RafalDorsz · Sep 14, 2014 at 07:18 PM
How fast are your bullets moving? For very fast moving objects, the physics don't always work - the rigidbody might move too far between frames. Even if that is not the case, I find that OnTriggerEnter() is not particularly reliable.
Edit: You may also try setting collision detection on your rigidbodies to Continuous and using Intrpolation. Also, playing with physics steps in the settings might help (or make things worse;) )
Very often, for shooting fast projectiles over short distances (where wind, gravity, and other effects can be approximated by small constants) hit detection is done using raycasting from the barrel, and bullets, tracer trails etc are just graphics effects. If that doesn't work for you, consider a hybrid method - raycast forward a small distance from the flying bullet and use that to determine if the bullet is about to hit something.
I'm using the hybrid method with the rigidbody raycast now. Works great, thank you!
changing to Continuous seems to have worked. thank you!
Answer by TagnumElite · Sep 14, 2014 at 06:35 PM
Try pitting it in a public void called Update
What is the method for checking the collision (as alternative to the function)?
Your answer
Follow this Question
Related Questions
Character Rotation Doesn't work 0 Answers
hey here wesley again sound for my gun 1 Answer
FPS Gun Attached To Camera Weirdness 3 Answers
Setting Without Altering Object Scale and Collider Scale 0 Answers