Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Ignore Collision not working

Discussion in 'Scripting' started by jwalden, Nov 23, 2017.

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Hey guys, my ignore collision isn't working

    The setup:

    2D Isometric game

    Objects:
    Projectile Prefab with projectile behaviour script (which direction and how fast)
    Player Object
    Both of the above have Box Collider 2D and Rigidbody2D attached
    Shot Origin - Empty game object located at the centre of the player.

    I need the Projectile box collider 2D to ignore the player collider 2D so that it doesn't clash, and just goes straight out from the player.

    CollisionManager - Empty game object with the below script attached:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IgnoreCol : MonoBehaviour {
    6.  
    7.     public GameObject playerCol;
    8.     public GameObject pProjectileCol;
    9.        
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.  
    16.        
    17.  
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         Physics2D.IgnoreCollision(pProjectileCol.GetComponent<Collider2D>(), playerCol.GetComponent<Collider2D>());
    24.     }
    25. }
    Also tried putting a the ignore collision in the projectileBehaviour script that I had. Made no difference.

    Not sure what's going wrong; would greatly appreciate any help/advice!!

    Thanks.
     
  2. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    the api docs show the function used in start
     
  3. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Doesn't matter if in Start or Update. Tried both and got same results.
     
  4. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17
    Hello.

    Is your projectile instantiated in your player when you shoot?
    if yes IgnoreCollision() must be called after the instantiate.

    example (using 3d coll)

    Code (CSharp):
    1. private void Fire(GameObject whoUseMe)
    2.     {
    3.         Projectile bullet = Instantiate(m_ProjectileRef, transform.position, this.transform.rotation) as Projectile;
    4.         bullet.InitializeProjectile(m_TagList, m_WeaponDamage);
    5.         bullet.transform.Rotate(0, Random.Range(-m_WeaponSpread, m_WeaponSpread),0);
    6.         bullet.gameObject.GetComponent<Rigidbody>().velocity = (bullet.transform.forward * m_WeaponProjectileSpeed);
    7.         Physics.IgnoreCollision(whoUseMe.GetComponent<Collider>(), bullet.GetComponent<Collider>());
    8.         Destroy(bullet, m_DeleteProjectileAfter);
    9.     }
    If not (this mean that you only have 1 projectile in your entiere game and projectile and player are set in your IgnoreCol class ) I dont know because i was not able to reproduce it.