How can I make rigidbody ignore some kind of collisions

I have a situation which probably many others have faced, I got a player, that player has a rigidbody and collision, this player shoots from a weapon bullets which have rigidbody and collisions as well. These bullets are meant to hit other players.

This all sounds pretty simple, but problem is, that given that I spawn the bullets in a middle of transform of the player who shoots, they immediately collide with the player who shot them out. I would like to make the bullets ignore the collision of their owner (the player who shot them).

How can I do that?

P.S. Some of you might suggest “why spawn them in a middle of player?” That is related to another question I made How to calculate position where bullet should be spawned - Unity Answers the answer is, that it’s not that simple to spawn them anywhere else in order to achieve this what I need.

how about spawn them as triggers and not colliders,
and then onTriggerExit change them to collider (trigger.enable = false)

You can use unity’s built in matrix system that allows you to chose what layers can interact with others.

For that case, I suggest you have a look at this:

As soon as you instantiate the bullet, call the Physics.IgnoreCollision and pass the 2 colliders (the collider of the bullet itself and the collider of the player). So the collisions between the 2 colliders will be ignored and therefore the bullet will pass through the player’s collider.

Hope this helps.

Well, in general your player can collide with pretty much anything (not only bullets), so the best way is to only perform code on specific bullets. You have the Collision parameter for this.

Something like that:

void OnCollisionEnter(Collision col)
{
	if(col.gameObject.name == "bullet_player1")
	{
		// STUFF
	}
}

You can use this code.

 private void OnCollisionEnter(Collision collision)
    {
        var collidedObject = collision.gameObject.GetComponent<WorkerAI>();
        if (collidedObject == null) return;
        Physics.IgnoreCollision(collidedObject.gameObject.GetComponent<BoxCollider>(),
            gameObject.GetComponent<BoxCollider>());
    }

“WorkerAI” - this can be any component on gameobject