Search Unity

Layermask not working

Discussion in 'Scripting' started by NintendoMaster00, Feb 13, 2016.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I have a layermask set to just ignore the Player, the player is in the Player Layer(boy this is a tongue twister)

    anyway here is my code
    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(position, Vector2.up,RayLength, ignorePlayer);
    2.             {
    3.                 Debug.Log("You have hit: " + hit.collider.name);
    4.             }
    the ignorePlayer is the Layermask

    and the Debug is saying hit Player
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Are you sure you're bot ignoring everything except for player?
     
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    This is what it looks like LayerMask.JPG this is my first time using layermasks but I think that you are supposed to select the layer you want ignored from the drop down (if not please tell me).
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Nope, do a:
    Code (CSharp):
    1. LayerMask ignorePlayer = ~(1 << ignorePlayer);
    This will basically invert the layerMask
     
  5. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    ok so is the 1 mean the first layer or the number of layers that are masked and is << ignorePlayer the Layer name?
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  7. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    thanks but the 1 problem is is that i get this error
    error CS0236: A field initializer cannot reference the nonstatic field, method, or property
    and if I change it to static the error goes away but the layermask does not work :p
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Could you show the function, that does this thing?
     
  9. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    OK so here is my updated script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Interaction_base : MonoBehaviour
    5. {
    6.     public PlayerMovement playermovement;
    7.     public float RayLength = Mathf.Sqrt(2);//How far the raycast will go
    8.     Rigidbody2D player;
    9.  
    10.     void Start()
    11.     {
    12.         player = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Debug.DrawLine(player.position, player.position + playermovement.Direction * RayLength, Color.red); // Shows where the raycast is in the inspector
    18.  
    19.    
    20.         if (Input.GetKeyDown("space"))
    21.         {
    22.             Vector2 position = player.position;
    23.             RaycastHit2D hit = Physics2D.Raycast(position, player.position + playermovement.Direction * RayLength,~1 << 9);
    24.             {
    25.                 Debug.Log("You have hit: " + hit.collider.name);
    26.             }
    27.         }
    28.     }
    29. }
    EDITED: oh and here are my layers: I want every layer to respond with the collider .name except the player layer also edited script to be ~1<< 9
     

    Attached Files:

    Last edited: Feb 13, 2016
  10. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    sorry I did not really reply to that Ok here is the error
    Assets/Scripts/Interaction_base.cs(12,48): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Interaction_base.ignorePlayer'

    and it is referring to
    Code (CSharp):
    1.     public LayerMask ignorePlayer = ~(1 << ignorePlayer);
     
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Do this:
    Code (CSharp):
    1. at the variables:
    2. public LayerMask ignorePlayer;
    3.  
    4. in Start:
    5. ignorePlayer = ~(1<<ignorePlayer);
    You didn't initialize the variable and you're trying to use it
     
    NintendoMaster00 likes this.
  12. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Thanks I cant believe I forgot something so basic!