Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeyex · Sep 14, 2014 at 05:46 PM · ontriggerentergunbooridgidbody

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeyex · Sep 14, 2014 at 07:58 PM 0
Share

I'm using the hybrid method with the rigidbody raycast now. Works great, thank you!

avatar image daichiy89 · Jun 11, 2020 at 06:31 AM 0
Share

changing to Continuous seems to have worked. thank you!

avatar image
0

Answer by TagnumElite · Sep 14, 2014 at 06:35 PM

Try pitting it in a public void called Update

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeyex · Sep 14, 2014 at 06:38 PM 0
Share

What is the method for checking the collision (as alternative to the function)?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Gaussian Distribution in circle on x and y axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges