- Home /
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!
Answer by duck · Nov 18, 2009 at 10:06 PM
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.
Answer by Leedsy · Nov 28, 2013 at 04:11 AM
For those who stumble upon this question since the more recent versions of Unity...
You can achieve group collision ignoring by going to Project Settings, and then either Physics or Physics 2D. In the Inspector window, you'll see a Layer Collision Matrix, where you can check or uncheck which layers you want to collide with each other.
Then, of course, just make sure your game objects are assigned to the correct layer!
Stumbled on this and this is the way to go. Thanks! I'd upvote but the forums won't let me :/
Answer by Sam Bauwens · Nov 19, 2009 at 08:31 AM
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 ^^.
Answer by HiddenMonk · Jun 03, 2015 at 12:13 AM
I use this
using UnityEngine;
public static partial class ExtPhysics
{
#region IgnoreCollision
public static void IgnoreCollision(params Collider[] colliders)
{
IgnoreCollision(true, colliders);
}
public static void AllowCollision(params Collider[] colliders)
{
IgnoreCollision(false, colliders);
}
public static void IgnoreCollision(bool ignore, params Collider[] colliders)
{
for(int i = 0; i < colliders.Length - 1; i++)
{
for(int j = i + 1; j < colliders.Length; j++)
{
Physics.IgnoreCollision(colliders[i], colliders[j], ignore);
}
}
}
#endregion
}
Example of its usage
using UnityEngine;
public class TestIgnoreCollisions : MonoBehaviour
{
public Collider[] colliders;
public bool ignore;
void Update()
{
if(ignore) ExtPhysics.IgnoreCollision(colliders);
if(!ignore) ExtPhysics.AllowCollision(colliders);
}
}
Note that although I am calling this every Update, its only for demonstration purposes. You should only call it when needed.
Answer by pablo · Nov 18, 2009 at 10:27 PM
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...
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.
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
how to capsule cast from within Entities.ForEach job? ECS 1 Answer
Lifting box 2D C# 0 Answers
Physics not working. 2 Answers
Keep Horizontal Momentum after Jump 2 Answers