How do I make the enemy stop walking and shoot the player?

The enemy still patrols while shooting if it sees the player

Patrolling Script

public float moveSpeed;
public bool moveRight;

public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatIsWall;
private bool hittingWall;

private bool notAtEdge;
public Transform edgeCheck;

void Start()
{

}

// Update is called once per frame
void Update()
{
    hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);

    notAtEdge = Physics2D.OverlapCircle(edgeCheck.position, wallCheckRadius, whatIsWall);

    if (hittingWall || !notAtEdge)
        moveRight = !moveRight;

    if (moveRight)
    {
        transform.localScale = new Vector3(-0.75f, 0.75f, 0.8f);
        GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
    }

    else
    {
        transform.localScale = new Vector3(0.75f, 0.75f, 0.8f);
        GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);

    }

Our “ShootAPlayerInRange” Script

public float playerRange;

public GameObject enemyBullet;

public CharacterController2D player;

public Transform launchPoint;

public float waitBetweenShots;
private float shotCounter;



// Start is called before the first frame update
void Start()
{
    player = FindObjectOfType<CharacterController2D>();
    shotCounter = waitBetweenShots;

}

// Update is called once per frame
void Update()
{

    Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), (new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z)));
    shotCounter -= Time.deltaTime;

    if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && shotCounter < 0)
    {
        Instantiate(enemyBullet, launchPoint.position, launchPoint.rotation);
        shotCounter = waitBetweenShots;
    }

    

    if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && shotCounter < 0)
    {
        Instantiate(enemyBullet, launchPoint.position, launchPoint.rotation);
        shotCounter = waitBetweenShots;
    }


}

}

Here’s one solution (not necessarily the best one):

In the ShootAPlayerInRange script:

Add the field public bool shootingAtPlayer

At the top of the Update method, add the code shootingAtPlayer = false;

In both If-statements, add the code shootingAtPlayer = true;


In the Patrolling script:

Add the field private ShootAPlayerInRange shootAPlayerInRageComponent

In the Start method, add the code:
`

shootAPlayerInRageComponent = GetComponent<ShootAPlayerInRange>();

`

In the Update method, add an if-statement that sets the object’s velocity to zero if shootAPlayerInRageComponent.shootingAtPlayer is true.


I would also recommend that you cache a reference to your Rigidbody2D. You are currently calling GetComponent every frame (twice even).