Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
14
Question by Galaroth · Apr 14, 2014 at 08:47 PM · collisionprojectileignorecollision

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

10 Replies

· Add your reply
  • Sort: 
avatar image
46

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.

alt text

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_1905612 · Jul 11, 2020 at 05:55 AM 0
Share

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?

avatar image hjc_ unity_1905612 · Jul 11, 2020 at 10:03 AM 0
Share

@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.

avatar image Moonwatchereclipse · May 26, 2021 at 01:48 AM 0
Share

holy heck tysm man

avatar image
19

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Datapax · Jan 08, 2017 at 07:11 PM 2
Share

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);
      }


: )

avatar image Mangospank · May 23, 2017 at 01:21 AM 1
Share

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);
       }
avatar image Datapax Mangospank · May 23, 2017 at 02:47 PM 0
Share

$$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);
        }
 }
avatar image phobos2077 · Sep 16, 2018 at 10:29 AM 6
Share

This is not a solution. Calling IgnoreCollision in OnCollisionEnter does not cancel the initial collision, so objects still receive collision impulses.

avatar image Babelguppy · Jun 05, 2020 at 10:16 PM 1
Share

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.

avatar image INK_Champ Babelguppy · Jul 18, 2020 at 10:26 PM 0
Share

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.

avatar image Moonwatchereclipse Babelguppy · Jun 07, 2021 at 10:08 AM 0
Share

you are epic

avatar image VirtualRubik · Nov 15, 2021 at 10:08 PM 0
Share

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);
         }
     }
 }
avatar image
9

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Manichus · Mar 08, 2021 at 09:28 PM 0
Share

Simple, concise, extensible. Thanks, just what I needed to find!

avatar image TheCodingTnT · Mar 12, 2021 at 02:54 PM 0
Share

This works perfect, thanks.

avatar image Y2KExtacy · May 06 at 10:07 PM 0
Share

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>());
     }
 
avatar image
7

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Galaroth · Apr 14, 2014 at 09:52 PM 0
Share

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

avatar image Galaroth · Apr 14, 2014 at 10:12 PM 0
Share

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.

avatar image Galaroth · Apr 14, 2014 at 10:20 PM 0
Share

No. It did not work. The same thing as before; the player collides with the projectiles.

avatar image robertbu · Apr 14, 2014 at 10:42 PM 0
Share

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


avatar image Galaroth · Apr 15, 2014 at 09:42 AM 0
Share

YES! It works. Using the matrix to ignore collision between the two layers. Thanks for help.

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KevRev · Apr 16, 2019 at 10:47 AM 0
Share

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);

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IgnoreLayerCollision not working 0 Answers

Damaging Enemy strangely not working. 0 Answers

Need help ignoring collision 1 Answer

Fired projectile collision with player issue 1 Answer

Can I make an Enemy Insatiate a projectile object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges