x


Grouping objects for collision ignoring.

Hi! I know that i can have two object not to collide by using physics.ignorecollision.

But what i would like to do is have many objects of a same class not to collide between each other... isn't there a way to make a collision group so this objects don't collide?

If it is not posible how can i call physics.ignorecollision for all of them in an efficient fashion?

Thanks!

more ▼

asked Nov 18 '09 at 09:14 PM

pablo gravatar image

pablo
14 6 6 8

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

4 answers: sort newest

There's currently no built-in way.

There's a feature request for this on the Unity Feedback Forum which you could add your vote to: http://feedback.unity3d.com/pages/15792-unity/suggestions/163341-physics-layer-based-ignore-collision

In the meantime, you could use this neat collision manager script on the wiki, written by Ryan Scott.

more ▼

answered Nov 18 '09 at 10:06 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

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

This is kind of the same question as this one, which I think I've answered, although it has actually been asked later than this one ^^.

more ▼

answered Nov 19 '09 at 08:31 AM

Sam Bauwens gravatar image

Sam Bauwens
145 3 3 12

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

I used this with Kinematic Rigidbodies (that weren't controlled by physics) and used OnTriggerEnter instead of OnCollisionEnter, but it may work in this case also.

If you're looking to make all objects of the same class not collide you could create a new "myClass" tag for them and in the OnCollisionEnter function add:

function OnCollisionEnter (col : Collider) {
	if (col.gameObject.tag == "myClass") {
	} else {
		Destroy(gameObject);
	}
}


If you want only some instances of this class ignore and not others, you can change that to:

function OnCollisionEnter (col : Collider) {
	if (col.gameObject.tag == "myClass") {
		if (!ignoreCollision) {
			Destroy(gameObject);
		}
	} else {
		Destroy(gameObject);
	}
}

then add the ignoreCollision variable to the class' script:

private var ignoreCollision : boolean = false;

To set an instance of the class to be ignored, you can create a function in the class' script:

function SetIgnoreCollision() {
ignoreCollision = false;
}

and then call it upon instantiation in whatever script handles that:

newMyClass = Instantiate(myClass, Vector3(0,0,0), Quaternion.identity);
newMyClass.SendMessage("SetIgnoreCollision");
more ▼

answered Nov 18 '09 at 10:34 PM

Design3.com gravatar image

Design3.com
655 1 1 11

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

Right now i am doing this:

function OnCollisionEnter(collision : Collision) {

if (collision.collider) {
	if(collision.gameObject.tag == "Bullet")
	{
		Physics.IgnoreCollision(rigidBody.collider,collision.collider);

	}
	if(collision.gameObject.tag == "Player")
	{
	}
}

}

Don't know if that is "efficient" but works...

more ▼

answered Nov 18 '09 at 10:27 PM

pablo gravatar image

pablo
14 6 6 8

It does? I thought doing that within 'OnCollisionEnter' would be too late - i.e. the collision has already occurred by that point. It would ignore any further collisions, but the first would always register.

Nov 18 '09 at 10:32 PM duck ♦♦
(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:

x1948

asked: Nov 18 '09 at 09:14 PM

Seen: 4417 times

Last Updated: Nov 18 '09 at 09:14 PM