Skip to main content
added range length to Debug ray
Source Link
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            //Draw a debug ray to see what's happening
            Debug.DrawRay(transform.position, m_RayDirection * m_RayRange, Color.green,.5f);

            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            //Draw a debug ray to see what's happening
            Debug.DrawRay(transform.position, m_RayDirection, Color.green,.5f);

            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            //Draw a debug ray to see what's happening
            Debug.DrawRay(transform.position, m_RayDirection * m_RayRange, Color.green,.5f);

            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
added Debug.DrawRay to script per OP's additional question in comments.
Source Link
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);
 

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            //Draw a debug ray to see what's happening
            Debug.DrawRay(transform.position, m_RayDirection, Color.green,.5f);

            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);
 

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);

        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            //Draw a debug ray to see what's happening
            Debug.DrawRay(transform.position, m_RayDirection, Color.green,.5f);

            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}
Source Link

Here is a demo that accomplishes what you are asking.

First, the set up -- the scene consists of a player sprite and 3 enemy sprites.

set up

The enemy sprites are assigned to the Enemy Layer. Each has a Collider2D component.

Enemy -- Inspector

The Player sprite is similar, but it also has a custom component attached and of course is not assigned to the Enemy layer.

Player -- Inspector

On to the script. First we declare a couple of handy variables with sensible default values that we can adjust later in the inspector:

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

On Start, we let LayerMask do the bit-twiddling to calculate the correct mask value:

 int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    } 

The fun happens in Update. First, we'll move our player from side to side, simply for demo purposes. Mathf.PingPong() is a simple way to do this.

transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);

Now, we test for a hit using the parameters we've assembled:

   RaycastHit2D hit;
   hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }

Here's the entire script:

using UnityEngine;

public class Player_X : MonoBehaviour
{

    [SerializeField] private Vector2 m_RayDirection = Vector2.down;
    [SerializeField] private float m_RayRange = 4;

    int enemyLayer;
    void Start()
    {
       enemyLayer = LayerMask.GetMask("Enemy");
    }

    RaycastHit2D hit;
    void Update()
    {
        //Move the Player back and forth across the screen, for demo
        transform.position = new Vector3(Mathf.PingPong(Time.time, 4f) - 2.0f, 0, 0);


        hit = Physics2D.Raycast(transform.position, m_RayDirection, m_RayRange, enemyLayer);
        if (hit)
        {
            Debug.Log("Hit " + hit.collider.name);
            //You could do other stuff here, like instantiate particles or play sounds...

            //Destroy the enemy GO:
            Destroy(hit.collider.gameObject);
        }
    }
}