Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

help ? Physics2D.IgnoreCollision ?

Discussion in '2D' started by andy30929, Apr 3, 2020.

  1. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class shooting : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public Transform firepoint;
    9.     public GameObject bulletPrefab;
    10.  
    11.     public GameObject bullet;
    12.  
    13.     public float bulletforce = 100f;
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if(Input.GetButtonDown("Fire1"))
    18.         {
    19.             Shoot();
    20.         }
    21.     }
    22.  
    23.     void Shoot()
    24.     {
    25.         bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
    26.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    27.         rb.AddForce(firepoint.up * bulletforce, ForceMode2D.Impulse);
    28.     }
    29.     void Start()
    30.     {
    31.  
    32.         Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), bullet.GetComponent<Collider2D>());
    33.     }
    34. }
    35.  
    I want to make the bullet will not Collison with other bullet
    // Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), bullet.GetComponent<Collider2D>());
    but this does not work...
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You've got a public "GameObject bullet" but what is this set to when it starts? Nothing? If so, I'm surprise you don't get any errors because immediately upon start you then try to get it to ignore the first collider on it with the same collider i.e. itself. That makes no sense.

    You have a private "Shoot()" method that is called anytime you fire but it replaces your public "bullet" field. It looks like you don't need that public field at all.

    May I ask if you are new to Unity/scripting? I'm assuming you are so forgive me if the following is stuff you already know.

    If you don't want "bullets" to contact other "bullets" then you should create a "Bullet" layer. Assign the bullet prefab to that layer. Then you can go into "Menu > Edit > Project Settings > Physics 2D". There you'll see a "Layer Collision Matrix". Ensure that the row/column where your new "Bullet" layer intersect is unchecked. This tells 2D physics that these should ignore each other.

    The "IgnoreCollision" doesn't need to be used here. This is for when you need to specify two specific colliders that should ignore each other. By the sound of it, you don't want this. You want all bullets to ignore each other."

    Here's a good overview of 2D physics.

    Hope this helps.
     
    andy30929 likes this.
  3. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    Thx, i get it. I am new to unity:D
     
    MelvMay likes this.