I am attempting to use a Raycast2D to detect if an attack has hit an enemy. I created a LayerMask for the “Enemy” layer and I have an object with a collider on the “Enemy” layer. I have no problem hitting the collider without using a LayerMask, however when I do, I get nothing back. What am I missing here?
LayerMask EnemyLayer;
void Start ()
{
EnemyLayer = LayerMask.NameToLayer ("Enemy");
}
public void BasicAttack()
{
PlayerVitality.Stamina = 0;
Vector2 attackDirection = CalculateAttackDirection ();
float attackDamage = CalculateDamage (BasicAttackDamage);
Debug.Log (EnemyLayer.value);
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x + 2, transform.position.y), attackDirection, BasicAttackDistance, EnemyLayer.value);
if (hit.collider != null)
{
Debug.Log("You hit: " + hit.collider.gameObject.name);
}
}