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. Dismiss Notice

Ignoring Collisions between Objects.

Discussion in 'Scripting' started by sepiormon, Jun 5, 2014.

  1. sepiormon

    sepiormon

    Joined:
    May 23, 2013
    Posts:
    10
    I have a Player that is a plane and shoots missiles, but the missiles keep bumping the plane. How would I solve this issue in JavaScript,

    I found a solution in C# : http://forum.unity3d.com/threads/ignore-collisions-by-tag-solved.60387/

    but I don't know how to convert it into JavaScript.

    Also, it is a 2D game, so I've tried to use: "Physics2D.IgnoreCollision(Player.collider2D, collider2D);" but still no results.

    Things I've tried:
    Code (JavaScript):
    1. function OnCollisionEnter2D (Other: Collision2D){
    2. if(Other.gameObject.tag == "Player"){
    3.         Physics2D.IgnoreCollision(Player.collider2D, collider2D);
    4.     }
    5. }
    Code (JavaScript):
    1. function Start (){
    2.     Physics2D.IgnoreCollision(Player.collider2D, collider2D);
    3. }

    Here is the script reference I've tried to understand with little success: http://docs.unity3d.com/ScriptReference/Physics2D.IgnoreCollision.html
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I suggest you look at the layer system. You can set in the physics settings which layers will interact with what.
    http://docs.unity3d.com/Manual/LayerBasedCollision.html

    For example, in your case you can put the player collisions on one layer and the enemy collisions on another. Then you can set it up so that enemies will only hit the player and not each other, or the player will only hit enemies and not interact with its own layer.
     
  3. sepiormon

    sepiormon

    Joined:
    May 23, 2013
    Posts:
    10
    If The Missile was in layer 9 in "Missile", and that player was in layer 8, "Player", what would I put in my code to make them not collide? I can'f figure it out,
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Nothing. You would setup the phsyics collision matrix so they ignore each other.

    its under Project->Physic Settings (or something along those lines)

    It wil be a grid of checkboxes with the layer names going along the top and side
     
  5. sepiormon

    sepiormon

    Joined:
    May 23, 2013
    Posts:
    10

    I was trying to use the matrix, and it didn't work..... Until I found out my Player was on the Default layer, thanks for your help, I appreciate it.