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;
}
}
}