Can someone Tell me how I can make the enemy shoot the player when the see them in 2D?

Hi, I’m currently making a small shoot em’ up game and I’ve hit a roadblock when trying to find a way for the enemy’s to shoot the player when they see them. I’ve tried everything (I think) and I’ve watched pretty much ever video on YouTube to try and find an answer. Could someone please tell me and/or give me a script to make the enemy shoot.

Thanks.

so if you can find a way to make the enemy to rotate towards the player so its always looking at the player if you have an existing shooting function you can copy it over to the enemy script and have it draw a circle around the enemy that when the player gets within that circle it starts the aiming function and then when the player gets within another circle that’s smaller it will start calling the shoot function.

I would give you all the code but its for a 3d enemy but the basic logic still applies if you can get anything out of the code here

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class enemy : MonoBehaviour
{
public NavMeshAgent AI;

public Transform player;

public LayerMask whatisground, whatisplayer, whatisbullet;

public float health;

//patrolling
public Vector3 walkpoint;
bool walkpointset;
public float walkpointrange;

//attacking
public float timebettweenattacks;
bool alreadyattacked;
public GameObject projectile;

//states
public float sightrange, attackrange;
public bool playerinsightrange, playerinattackrange;

private void Awake()
{
    player = GameObject.Find("player").transform;
    AI = GetComponent<NavMeshAgent>();
}

private void Update()
{
    //check for sifgt and attack range
    playerinsightrange = Physics.CheckSphere(transform.position, sightrange, whatisplayer);
    playerinattackrange = Physics.CheckSphere(transform.position, attackrange, whatisplayer);

    if (!playerinsightrange && !playerinattackrange)
    {
        patroling();
    }

    if (playerinsightrange && !playerinattackrange)
    {
        chaseplayer();
    }

    if (playerinsightrange && playerinattackrange)
    {
        attackplayer();
    }
}

private void patroling()
{
    if (!walkpointset)
    {
        searchwalkpoint();
    }

    if (walkpointset)
    {
        AI.SetDestination(walkpoint);
    }

    Vector3 distancetowalkpoint = transform.position - walkpoint;

    //walkpoint reached
    if (distancetowalkpoint.magnitude < 1f)
    {
        walkpointset = false;
    }
}

private void searchwalkpoint()
{
    //calculate random point in range
    float randomz = Random.Range(-walkpointrange, walkpointrange);
    float randomx = Random.Range(-walkpointrange, walkpointrange);
    walkpoint = new Vector3(transform.position.x + randomx, transform.position.y, transform.position.z + randomz);
    if (Physics.Raycast(walkpoint, -transform.up, 2f, whatisground))
    {
        walkpointset = true;
    }
}

private void chaseplayer()
{
    AI.SetDestination(player.position);
}

private void attackplayer()
{
    //attack code here
    Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();

    rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
    rb.AddForce(transform.up * 8f, ForceMode.Impulse);

    //make sure your enemy doesnt move
    AI.SetDestination(transform.position);

    transform.LookAt(player);

    if (!alreadyattacked)
    {
        alreadyattacked = true;
        Invoke(nameof(resetattack), timebettweenattacks);
    }
}

private void resetattack()
{
    alreadyattacked = false;
}

public void takedamage(int damage)
{
    health -= damage;

    if (health <= 0) Invoke(nameof(destroyenemy), 0.5f);
}

private void destroyenemy()
{
    Destroy(gameObject);
}

private void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(transform.position, attackrange);
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere(transform.position, sightrange);
}

}

hope this helps