x


How can I make two colliders pass through each other?

Hello, I am making a game with a character who has four types of bullets, one of which gives a small damage but passes through the enemy. My problem here was that whenever I fired the bullet it stuck to the first enemy itself and destroyed itself after the time I set. To counter this I thought maybe I could set the gameObject.isTrigger = true whenever the collision happens, so the bullet would pass through it and when it is out of the object I set gameObject.isTrigger = false, so that it would turn on collision physics again. Now whenever I run the game and fire that bullet, it hits the first enemy and then unity simply crashes. Because of that, I haven't been able to find the error as well. Can you please suggest any other method that might be more successful, or a correction for the code I used which I have written below,

function OnCollisionEnter(bullet : Collision){  

if(bullet.gameObject.name == "stunbullet"){
//stun bullet code
}
else if(bullet.gameObject.name == "damagebullet"){ //damage bullet code } else if(bullet.gameObject.name == "pushbackbullet"){
//push back bullet code
}
else if(bullet.gameObject.name == "piercebullet"){
ApplyDamage(1) //Code to Apply Damage on the enemy object
while(bullet.gameObject.name == "piercebullet"){
gameObject.collider.isTrigger = true;
}
gameObject.collider.isTrigger = false;
}
}

all the other types of bullets are working fine except the pierce bullet which should go through the enemies.

Help would be most appreciated

more ▼

asked Jul 31 '12 at 02:02 PM

Niluk93 gravatar image

Niluk93
7 1 2 4

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

2 answers: sort voted first

use Physics.IgnoreCollision

whole thing is worked out for you, with that method. Documentation can be found here

more ▼

answered Jul 31 '12 at 03:42 PM

DESTRUKTORR gravatar image

DESTRUKTORR
851 3 5 11

worked perfectly. Thanks a lot.

Jul 31 '12 at 05:34 PM Niluk93

No prob :)

Jul 31 '12 at 08:55 PM DESTRUKTORR

how did u manage to decrease the enemy's health if you ignore collisions between him and the bullet?

Oct 17 '12 at 12:42 PM masabusharif

You don't need a call from a collision or entry of a trigger to detect if something is within the bounds of the object. Just check if the distance is short enough, or use a raycast (the former is a slight bit more complicated, admittedly, but it would be a lot less costly to perform than the latter).

And yes, I realize this was posted over a year later... XP

Mar 28 at 01:30 PM DESTRUKTORR

Its ok, i appreciate your response :), well the solution for what i wanted was checking for distance, and everything went perfectly, thank you

Apr 01 at 08:19 AM masabusharif
(comments are locked)
10|3000 characters needed characters left

The only code you even have is in the pierce bullet section, and it's a while loop that can never exit. A while loop runs until its condition becomes false and that condition can never be false inside that loop.

If you want to use a while loop it either needs to yield or eventually make its condition false:

while ( true ) { x++; yield; }

while ( x < 10 ) { x++; }

Ignore collision prevents collision from happening. The best way to ignore collision between groups of objects is to put their prefabs on different physics layers and then set the project's physics settings so those layers don't collide.

Triggers are able to pass through each other without colliding but you need to use OnTriggerEnter instead of OnCollisionEnter.

more ▼

answered Jul 31 '12 at 04:03 PM

Loius gravatar image

Loius
10.9k 1 12 43

Vincenti: There was no code in the other sections was because it was working fine and I didn't want to type it all.

Jul 31 '12 at 05:34 PM Niluk93
(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:

x5273
x1948
x293
x161

asked: Jul 31 '12 at 02:02 PM

Seen: 870 times

Last Updated: Apr 01 at 08:19 AM