x


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?

more ▼

asked Mar 28 '10 at 06:53 PM

The BOOM gravatar image

The BOOM
283 20 26 39

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Jul 19 '10 at 03:14 PM

NZRoadKill gravatar image

NZRoadKill
137 2 3 8

(comments are locked)
10|3000 characters needed characters left

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.

==

more ▼

answered May 23 '10 at 10:56 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Mar 28 '10 at 07:18 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 117 165

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.

Mar 28 '10 at 08:26 PM The BOOM

Oh, i see. My bad! Hmm.

Mar 28 '10 at 08:28 PM e.bonneville

need to see the code

Apr 25 '10 at 11:55 PM spinaljack

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

May 09 '10 at 09:34 PM Peter G
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1864
x1765
x1020
x102

asked: Mar 28 '10 at 06:53 PM

Seen: 4370 times

Last Updated: Mar 28 '10 at 06:53 PM