- Home /
How do I use Physics.IgnoreCollision with a tag?
I am new Unity user who is working on learning Javascript.
I have an object instantiating rocks which is set to IgnoreCollision with the root object which is the player the current object is parented to. The following code works in my scene.
if(Input.GetButtonUp("Fire1") && invRock){
var newRock : Rigidbody = Instantiate(rockObj, transform.position, transform.rotation);
newRock.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, force));
Physics.IgnoreCollision(transform.root.collider, newRock.collider, true);
}
I also have a series of invisble walls all tagged "InvisWall" which are set to stop the player, but I don't want it to stop any rocks thrown. Can I add another Physics.IgnoreCollision that references all the objects tagged "InvisWall", and how would I format that?
Thank you!
Answer by jashan · Jul 26, 2010 at 07:54 AM
Physics.IgnoreCollision with tags is not supported. However, Unity 3 will introduce layer based ignore collisions. So with Unity 3 you will be able to ignore collisions based on layers.
You could, of course, probably get lists of objects with a specific tag and then implement "tag based ignore collisions" yourself by setting ignore collision for all the relevant objects in a loop. But I guess I'd rather wait for Unity 3 (unless your project is late in its release cycle ;-) ).
Answer by SteveTwo · Jul 27, 2010 at 05:17 AM
I was able to solve the problem by working with a piece of code from the Unity forums.
This small script is applied to the rock prefab. It cycles through all objects sharing the tag specified when the prefab is instantiated and adds ignorecollision for each one.
function Start () { ignoreCollision("InvisWall"); }
function ignoreCollision(tag : String) { var objects = GameObject.FindGameObjectsWithTag(tag); for (o in objects) { if (o.GetComponent("Collider") && o != gameObject) Physics.IgnoreCollision(collider, o.collider); } }
Your answer
Follow this Question
Related Questions
Physics.ignoreCollision and Animations: Not playing nicely together 1 Answer
Ignore Collision problem 1 Answer
What's wrong with OnCollisionEnter? 2 Answers
Physics.IgnoreCollision not working 0 Answers
ignorecollision return error 0 Answers