- Home /
How do i get some objects to ignore collision with a specific object?
I'm making a 2D platformer, and my player can shoot projectiles. I don't want the projectiles to be able to collide with the player, have tried some different things, but can't get it to work. Can anyone tell me how to accomplish this?
Answer by Vandell · Jun 21, 2018 at 06:53 PM
You could use the Layer Collision Matrix from Edit > Project Settings > Physics to do that without scripting.
Create a layer (e.g Layer1) and disable the Layer1/Layer1 collision. Also, assign your objects to this layer, of course.
Suppose that i have few objects outside a huge box. The box and all the objects have collider components on them. I want those objects to be able to enter the box (without any change to their velocity) and once they enter, they should get collided back into the box whenever they collide with the huge box.
I.e. i want to ignore the first collision so that objects can enter.
How can i achieve this?
@unity_1905612 - you should post your own question, but you can achieve this by using both triggers and colliders. For the huge box, when the small objects are on the way in, use triggers, then swap to colliders once inside.
Answer by oldschoolj · Apr 14, 2014 at 11:16 PM
It's very simple.
Simply take the object(s) that you want to be ignored, and give them a tag, or layer. Now the method of exactly "how" to ignore them is different based on what you are doing, but all you need to do is tell the object that the script is attached to (the one you want to be the "ignorer", to do so when it encounters any object with the tag, or layer you set up.
If using a tag: void OnCollisionEnter(Collider collision) if(collision.gameObject.tag == "theobjectToIgnore" )
If using a layer: void OnCollisionEnter(Collider collision) if(collision.gameObject.layer == "theobjectToIgnore" )
and I would make sure that object to be ignored has a rigid body.
TAG
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
LAYER
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
: )
How would one start the script for the following? I'm not great at code and have tried finding the answer but have had no luck. Any help with that would be very much appreciated!
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
$$anonymous$$angospank, you should attach the script to a game object with a collider. The script should look like this:
using UnityEngine;
using System.Collections;
public class Collision : $$anonymous$$onoBehaviour
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
}
This is not a solution. Calling IgnoreCollision in OnCollisionEnter does not cancel the initial collision, so objects still receive collision impulses.
This helped me, once I changed all the Physics etc. to 2D. I'm not working with shooters, but basically need enemies to ignore everything but the player on their patrols in my game.
$$anonymous$$y variation with the updated syntax put in my Unity:
public string TagToIgnore = "Ignored";
void OnCollisionEnter2D(Collision2D collision){
if(collision.gameObject.tag == TagToIgnore ){
Physics2D.IgnoreCollision( collision.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
}
I just dump copies of this little diddy on my objects for each tag I want them to ignore. Not the most efficient, but simple and reusable.
Thank you so much! This worked really well for what I needed. Too bad I can't seem to get it completely ignore the player like as if there was no initial collision but it will have to do.
Thanks man, works perfectly.
using UnityEngine;
public class playerMovementHorizontal : MonoBehaviour {
public Rigidbody2D rb2d;
public float speed;
private void Update() {
rb2d.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb2d.velocity.y);
Debug.Log(transform.position);
}
private void OnCollisionEnter2D(Collision2D col2d) {
if (col2d.collider.name == "GroundCheck") {
Physics2D.IgnoreCollision(col2d.collider, col2d.otherCollider);
}
}
}
Answer by iqbalklv · Jul 13, 2020 at 06:05 PM
You are not supposed to put the IgnoreCollision on OnCollisionEnter as it will still detect the first ever collision, but you can put it on the start() function like this:
private void Start()
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
So, first you assign an object to the variable player, here I'm assigning it with the object with the tag "Player" then after that you just use the IgnoreCollision function like usual.
Simple, concise, extensible. Thanks, just what I needed to find!
I am slightly confused as to how this works. I have managed to make this work on some objects, but it doesn't work on others.
I am using this for a bullet, and here is my code:
public class BulletControllerModified : MonoBehaviour
{
public float bulletSpeed;
public Rigidbody2D theRigidBody;
public Vector2 moveDir;
public GameObject impactEffect;
//maybe needed for BulletIgnore tag
//public GameObject bullet;
public int damageAmount = 3;
// Start is called before the first frame update
void Start()
{
//ignoring collision with BulletIgnore tag
GameObject bullet = GameObject.FindGameObjectWithTag("BulletIgnore");
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
Answer by robertbu · Apr 14, 2014 at 08:51 PM
For 2D, you need to put the objects on separate layers. Then you can either set the collision matrix in:
Edit > Projects Settings > Physics2D
Or you can use Physics2D.IgnoreLayerCollision()
More info on the Physics2D Manager:
https://docs.unity3d.com/Documentation/Components/class-Physics2DManager.html
Have tried to use Physics.IgnoreCollision, it doesn't work. The projectiles still collide with the player.
Here's the part of the script. No errors or anything.
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "projectile")
{
Transform bullet = Instantiate(bulletPrefab) as Transform;
Physics.IgnoreCollision(bullet.collider, collider);
}
And have no idea of how the matrix you mentioned works
No bulletPrefab is a GameObject. How do i assign it as a Transform? (i'm guessing it has to be the transform of the projectiles). But the script is on the same object as the player collider yeah.
No. It did not work. The same thing as before; the player collides with the projectiles.
I should have looked at your code or read your question more carefully. IgnoreCollision() is for 3D colliders. You are making a 2D system. From a look at the reference (not by experience), it appears that you will have to put the bullets on a different layer. You can then use Physics2D.IgnoreLayerCollision(). Sorry for not getting it right up front. You can also set what layers collide using:
Edit > Project Settings > Physics2D
YES! It works. Using the matrix to ignore collision between the two layers. Thanks for help.
Answer by EvilPandaMan · Jan 22, 2018 at 05:48 PM
So I have tried the following as suggested.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
I'm trying to push a ball through a barrier, the ball is supposed to go through but not the character. The object is named Ball and the tag is also named Ball. So my script is as follows:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ball")
{
Physics.IgnoreCollision(**Ball**.collider, collider);
}
}
the second ball though is red and it is not being detected via typing... can anyone help me?
The second Ball should be collision.
Assu$$anonymous$$g this code is on the barrier, the OnCollisionEnter declaration is grabbing the Ball as an object named "collision".
The line should be:
Physics.IgnoreCollision(collision.collider, collider);