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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

LayerMask

Discussion in '2D' started by Yawuar, Mar 18, 2015.

  1. Yawuar

    Yawuar

    Joined:
    Mar 10, 2015
    Posts:
    6
    Hello

    My player has a Layermask so he cannot shoot himself.
    But the problem is when my enemy tries to shoot at the player, he doesn't get hit.
    The problem is the layermask obviously. But how can I solve this?

    The code :
    Player
    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D col) {
    2.                 if(col) {
    3.                     if(notHitPLayer == 0) {
    4.                         int live = 3;
    5.                         Health -= 10;
    6.                         Destroy(col.gameObject);
    7.                         Debug.Log("Bam = " + Health);
    8.                         if(Health <= 0) {
    9.                             isAlive = false;
    10.                             player_anim.SetBool("dying", true);
    11.                             StartCoroutine(DieAfterAnimation());
    12.                             lm.KillPlayer();
    13.                             live--;
    14.                             Debug.Log(live);
    15.                            
    16.                         }
    17.                     }
    18.                 }
    19.     }
    Enemy :

    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D col) {;
    2.         if(col) {
    3.             if(noHitEnemyAfterDie == 0) {
    4.                 Health -= 1;
    5.                 Destroy(col.gameObject);
    6.                 if(Health <= 0 && isAlive) {
    7.                     enemy_anim.SetBool("dying", true);
    8.                     isAlive = false;
    9.                     StartCoroutine(Die());
    10.                     ScoreManager score = levelManage.GetComponent<ScoreManager>();
    11.                     score.Score += 10;
    12.                    
    13.                     SpawnManager sm = levelManage.GetComponentInChildren<SpawnManager>();
    14.                     sm.SpawnEnemy();
    15.                 }
    16.             }
    17.         }
    18.     }
    Thanks.
     
  2. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    A Layermask is just a value that allows you to represent a layer or layers that you want to deal with. I don't see anything in your code that uses Layermasks. I can only guess that what you really mean to say is that you made changes to the layer collision matrix?
     
  3. Yawuar

    Yawuar

    Joined:
    Mar 10, 2015
    Posts:
    6
    Oow I'm sorry. "notHitPlayer" & "noHitEnemyAfterDie" are my layermasks
     
  4. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    I think you are confused about how layermasks work. It is just a value that represents 1 or more layers. It is often used in things like raycasts to indicate which layers should be included.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    You could make playerbullet and enemybullet to be different objects (if its the bullet that hits them)

    Or check Edit/Project Settings/Physics2D settings to see which layers collide each other.
     
    theANMATOR2b likes this.