Preventing colliders on the same rig from colliding with each other. But allowing them to collide with other rigs. - unity3d

I have a prefab NPC which has a physics rig attached to it (to do some specific rag-doll stuff with). I need to avoid the various colliders on the same rig (arms, legs etc) from colliding with each other, but they have to be able to collide with the rigs of other instantiated NPCs.
Is there a way to do this? I know I can avoid all the colliders from colliding by putting them on a separate layer, but I cant create a new layer for every NPC.
Thanks

You can do by setting up IgnoreCollision on your NPC class if you have any
http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
So simple loop through all colliders in the rig and set up to ignore each other
void Start() {
colliders = GetComponentsInChildren<Collider>();
foreach(Collider collider in colliders) {
otherColliders = GetComponentsInChildren<Collider>();
foreach(Collider otherColider in otherColliders) {
if (collider != otherColider) {
Physics.IgnoreCollision(collider, otherColider);
}
}
}
}

It looks like the only way to ignore collisions without using layers is to use Physics.IgnoreCollision() between a pair of colliders, for each pair.
You can write some code that would automatically register a newly instantiated game object and create these pairs between the new object and the other ones that were registered before, so you wouldn't need to call this method for each pair yourself.
Or, you can use this code that does that for you :) It has its own representation of layer to control how the objects should ignore each other.

Related

How do I spawn multiple bullets with rigidbodies in the same spot without them colliding

I am making a 2D game and have a basic weapon which I decided to call a pistol. I want to make a shotgun next and I have all of the work done except for the fact that they are all spawned in the same place and they all collide with each other.
I've had a few ideas like trying to turn off collisions for the rigidbodies (Didn't work), applying force without a rigidbody (Didn't work). I've done one game before this which was through a tutorial so this is my first real game and I just need help. If you need more details I can always send more. Thank you.
Jump into the Physics2D settings and make sure that the layer you’ve set for your “bullets” is not colliding with itself.
The one you’ll want to turn off for self collision is usually the one on the end, that intersects with itself on the row and column:
The script you need is the following line. In this line, Unity physics renders one collider ineffective against another.
Physics2D.IgnoreCollision(collider, ignoreCollider);
Also, with the algorithm below, you can neutralize all colliders at the moment of the bullet shot.
[SerializeField] private GameObject bulletType;
private List<Collider2D> _tempCollides;
void ShotBullets()
{
_tempCollides.Clear();
for (int i = 0; i < 4; i++)
{
// Instantiate and catch all colliders in a List
var collider = Instantiate(bulletType, AnyPlace, AnyRotation).GetComponent<Collider2D>();
_tempCollides.Add(collider);
}
foreach (var _collider in _tempCollides) // For Every Bullet Collider...
{
foreach (var _subCollider in _tempCollides) // .. Define to Ignore other Colliders..
{
if (_collider != _subCollider) Physics2D.IgnoreCollision(_collider, _subCollider);
}
}
}

Two collider in one game object how to distinguish between them when colliding with another?

I'm making a 2D game, there is one player with two collider : a box collider 2D on top and a small circle collider 2D bottom. When the player jump on the box he will be ok, but when he collide with the box with his face(box collider 2D) he will die ? How can I do that? I tried the following code , but it not work. Please help me.Thanks!
if (GetComponent<Collider2D>().GetType() == typeof(BoxCollider2D))
{
//do something
}
if (GetComponent<Collider2D>().GetType() == typeof(CircleCollider2D))
{
//do something
}
If you want to get a certain BoxCollider, you can use GetComponent<BoxCollider2D>() and GetComponent<CircleCollider2D>()
However, I am not sure how to take this information and check which one fired the the OnCollisionEnter, unless you set one collider to a trigger, and use OnTriggerEnter for one, and OnCollisionEnter for the other.
What I would recommend is having two child game objects on the player and put both colliders on each game object, and handle each event in their own OnCollisonEnter

How to detect two object collide or not?

I am making one unity game in which two objects having collider in which I have select isTriger and does not have rigid body, if I put the rigid body then they are kinematic object, So that gravity did not effect on that object, Even also i don't want any physic operation on this object also. but I want to detect whether this two object collide which each other or not.
How can I do this ?
When 2 colliders make contact with each other,
OnCollisionEnter2D
OnCollisionExit2D
OnCollisionStay2D
are called for 2D games, likewise for 3D(remove 2D in names) also.
Check out this link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
Sorry man, but unity use the phsysics engine to detect collision so you have to add the rigidbody to the item you want to to plug the script.
PS:remeber if you want to detect collisions with Trigger collider, you need to use
void OnTriggerEnter(){
//your code
}
void OnTriggerStay(){
//your code
}
void OnTriggerLeave(){
//your code
}

Getting collisions for multiple object without the collision callbacks

I'm making a platformer (sidescroller), and right now I'm making a grenade-launcher for the player.
It spawns grenades which is a prefab, this prefab has a Circle Collider 2D which is the blast radius for the explosion.
When a grenade is spawned I run this (amongst other things);
// Add the initial force
rBody.AddForce(new Vector2(forceX, forceY) *300f);
Invoke("Explode", 2.5f);
I'm having trouble figuring out how I should handle the Explode function.
I would like to find all the GameObjects that are colliding with the Circle Collider 2D at that point, but I can't find a way to do it.
I would like to be able to do something like this (not real code but you understand what I'm trying to do)
void Explode () {
collidingObjects = circleCollider.getCollisions();
foreach(collidingObject as entity) {
if(entity = 'player')
player.pushLeftOrRight()
elseif(entity = 'enemy')
grenade.dealDamage(grenade.damage)
}
Debug.Log("Explode");
Destroy(gameObject);
}
I'm guessing that it wouldn't work so can anyone point me in the right direction?
In your grenade's script, use OnTriggerEnter or OnCollisionEnter to log collisions; basically just have a HashSet for each grenade that is updated and keeps track of what it is colliding with. Then Explode just has to iterate through this set when you call it.

Disable or enable collisions based on object tag Unity 2d

I want to disable or enable collision with object in unity 2d game based on its tag. Lets say I have object with tag "foo1" and objects with tag "foo2". If user choose to collide with objects "foo1" then it should not collide with objects "foo2".
How could I achieve this? I tried this:
void OnCollisionEnter(Object other)
{
if (other.tag == "foo1")
collider.enabled = false;
}
But this is not working for two reasons. First object has to have isTrigger set to true (this could not be set for objects that serves as ground) and if I disable entire collider then object will fall through ground.
I am new to unity and I will study it in more details but I am asking for quick help and maybe idea how to do this?
Instead of doing this via tags, you might want to have a look at layers.
By assigning different objects to different layers, you can set them to either collide with each other, or ignore any potential collisions. You can achieve this at
Edit->Project Settings->Physics
where you can edit the layer collision matrix, to enable or disable collisions between layer elements.