[HELP ME] My Player AI Doesn't Stop Moving When In Range With Enemy!

HI, I wanted to make my player AI stop moving when it in range with my enemy AI.
Here is the script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class PlayerAIBehaviour : MonoBehaviour
{
    public float speed; //speed
    public float health; //health
    public float damage; //damage
    public float attackrange; //attackrange
    public bool ismoving;
    private Transform enemy;
    Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
        ismoving = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (ismoving = true)
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }
        float distanceFromEnemy = Vector2.Distance(enemy.position, transform.position);
       
        if (distanceFromEnemy < attackrange)
        {
            ismoving = false;
        }
    }
}

Your first if statement is wrong.

Wether: if (ismoving == true)
or just: if (ismoving)

2 Likes

OMG YOU’RE THE BEST!
IT WORK NOW!

1 Like