- Home /
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
Answer by DESTRUKTORR · Jul 31, 2012 at 03:42 PM
use Physics.IgnoreCollision
whole thing is worked out for you, with that method. Documentation can be found here
how did u manage to decrease the enemy's health if you ignore collisions between him and the bullet?
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
Its ok, i appreciate your response :), well the solution for what i wanted was checking for distance, and everything went perfectly, thank you
Answer by Loius · Jul 31, 2012 at 04:03 PM
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.
Vincenti: There was no code in the other sections was because it was working fine and I didn't want to type it all.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Using cylinder colliders as wheels. 1 Answer
Rigidbody Sliding UP Incline 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
I'm having troubles using a Capsule Cast 2 Answers