As the title suggests I would like to destroy the game object that my raycast hits. I have a movement script attached to my player object. A raycast2d is continuously shot downward, and when it collides with an object on the enemy layer it registers the collision and returns its value. What I want to do now is destroy that object, however when I use the destroy game object command, it destroys the gameObject that the script is attached to, as one may expect. How can I destroy this enemy object, which the script is not attached to.
This is the current code that I am using:
bool HitEnemy()
{
Vector2 position = transform.position;
Vector2 direction = Vector2.down;
float distance = 2.0f;
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, enemyLayer);
if (hit.collider != null)
{
return true;
}
return false;
}
I will be doing something similar, where my enemy shoots a horizontal ray, which will then kill the player character. How can I reference these 2?
I am relatively new to C# coding, so any help will be greatly appreciated.