• 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
1
Question by The-BOOM · Mar 28, 2010 at 06:53 PM · rigidbodycollidertriggertriggers

OnTriggerEnter problems

I have the function OnTriggerEnter on one of my bullet objects. The strange thing is, it works against one type of enemy when the enemy is moving, has a trigger, and has a rigidbody. It also works on another type when the enemy is stationary, has a trigger, and the bullet has the rigidbody. However, it does not work on both enemies at the same time. I have no idea why this is happening. The description of OnTriggerEnter is that only one object needs a rigidbody and only one needs a trigger enabled, but that isn't working for me. How can I get this to work?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by e.bonneville · Mar 28, 2010 at 07:18 PM

Well, if the enemy is flying towards you, and the bullet is flying towards the enemy, it's possible that the bullet is moving so fast compared to the enemy's velocity that it actually isn't entering the trigger at all - this can happen when the object is moving so fast that one frame, it's next to the enemy, and the next, has passed through it! There are several ways to fix this. Option 1 is to make the trigger bigger. Another (unacceptable in most cases) option is to decrease the speed of the bullet and the enemy. The third option is to use raycasts. This detects whatever is in front of the bullet, and tells it to explode several frames before actual contact. This code should do the trick - put it on your bullet:

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, Vector3.forward, hit, 5.0)) {
        //Put your explode script here.
    }
}

If it still isn't working, increase the size of your trigger, or increase the size of the third variable (the 5). Hope this helps!

Comment
Add comment · Show 4 · 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 The-BOOM · Mar 28, 2010 at 08:26 PM 0
Share

The problem isn't that it isn't hitting it's target. The problem is I can only code it to hit one target or another, not both.

avatar image e.bonneville · Mar 28, 2010 at 08:28 PM 0
Share

Oh, i see. $$anonymous$$y bad! Hmm.

avatar image spinaljack · Apr 25, 2010 at 11:55 PM 0
Share

need to see the code

avatar image Peter G · May 09, 2010 at 09:34 PM 0
Share

Can your show us your bullet code? It is hard to guess why without seeing any scripts.

avatar image
0

Answer by equalsequals · May 23, 2010 at 10:56 PM

I'm not sure if I am understanding this right, but you have a specific scenario where a single bullet can hit multiple enemies at the same time?

It looks to me as you are using the bullet to detect "hey, I hit something", again, I'm not sure if I am understanding correctly but maybe you should consider working it the opposite where your enemies say "hey, I was hit by something" and act accordingly.

With this method you will still be able to access things you need to do in the bullet's behaviour's as you will still have reference to the bullet (atleast its collider component) upon OnTriggerEnter()

Hope that helps.

==

Comment
Add comment · 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
0

Answer by NZRoadKill · Jul 19, 2010 at 03:14 PM

Check out this page. On there you will find an interaction chart that shows what type of combinations of colliders/triggers can interact. You may have an issue depending on your setup.

http://unity3d.com/support/documentation/Manual/Physics.html

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Simple Triggers problem. 1 Answer

Trigger doesn't work 100% of the time. 1 Answer

Is it possible to combine child trigger of a object to only trigger OnTriggerEnter once? 1 Answer

Trigger via Collider 1 Answer

Collision not working at all [ABANDONED] 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges