- Home /
Collision problem Ignorecollision
I have these objects that are being spawned. I do not want them to collide with reach other unless they are a certain height above the ground. Is it possible to ignore collisions between these objects until they have reaches a certain height? I don't want to turn off collisions completely because they are allow to collide with other objects in the scene. I gave them all the tag "Enemy". Thanks for any help in advance!
Answer by by0log1c · Jan 05, 2012 at 10:32 PM
Should have started there : Unity Scripting Reference : Physics.IgnoreCollision, heh.
Edit: Any script can call Physics.IgnoreCollision(collider1,collider2,state) at anytime. collider1 and collider2 are Collider component reference found through script using GetComponent() or dragged through the inspector. state is a boolean(true/fale) turning collision on or off.
var myCollider:Collider; var otherCollider:Collider;
function CollisionOff():void { Physics.IgnoreCollision(myCollider,otherCollider,false); }
Ok, that helps a lot. So these objects that are being instantiated are able to ignore each other? I am worried that when I use this script I will get an error saying the other object doesn't exist yet or it can't be the same object. Also am I able to refer to these colliders using tags?
Yes, indirectly. You'll first get the GameObjects using GameObject.FindGameObjectsWithTag("Enemy") then loop through the array and use GetComponent() to retrieve the collider. With that, call Physics.IgnoreCollision().
I have this all setup. Here is the issue, hopefully you can give some suggestions. I am getting an error saying that both myCollider and othercollider need to be active (I.E instantiated).
Since this isn't something I can control due to the nature of the game is there a way to check and see if these objects are in existance and then I can run this script in an If statement. Thanks for all the help so far
Answer by Sericet · Jan 05, 2012 at 10:44 PM
I have already read that and it didn't make sense to me. I am new to programming and wasn't sure how to do it for my senario. Your answer did not help.