- Home /
Problem is not reproducible or outdated
Enemy AI problems
I've been looking for a solution to this for a while, but most solutions don't have what I'm looking for. I'm trying to make a side-scrolling brawler, but I can't seem to find a way to make the enemy move towards the player when they are within a larger range, but move away from the player if they are within a smaller range.
The main controller script. Some sections are commented out, these are my original method of doing it, which worked, but only had the enemy charge towards the player.
public class EnemyController : MonoBehaviour
{
// public float minRange;
// public float maxRange;
// public float moveSpeed;
public float meleeRange;
public float meleeCooldown;
// private float targetDistance;
// private int range;
// private Vector2 targetVector;
private Rigidbody2D enemy_rb;
// private Rigidbody2D target_rb;
public GameObject enemyMelee;
// private GameObject target;
void Awake ()
{
enemyMelee.SetActive (false);
enemy_rb = GetComponent<Rigidbody2D>();
// target = GameObject.FindWithTag ("Player");
// target_rb = target.GetComponent<Rigidbody2D>();
// targetVector = enemy_rb.position - target_rb.position;
// targetDistance = targetVector.magnitude;
}
void Start ()
{
Rest ();
}
void FixedUpdate ()
{
// if (targetDistance <= meleeRange)
// {
// StartCoroutine (EnemyMelee (meleeCooldown));
// }
}
void LateUpdate ()
{
// if (targetDistance < minRange && targetDistance < maxRange)
// {
// range = 2;
// MoveEnemy ((-1 * moveSpeed), enemy_rb);
//
// if (targetDistance < maxRange && targetDistance > minRange)
// {
// range = 1;
// MoveEnemy (moveSpeed, enemy_rb);
// }
// }
}
void
public void MoveEnemy (float speed, Rigidbody2D rb)
{
Vector2 direction = Vector2.left;
rb.velocity = (direction * speed);
}
public void Rest ()
{
}
IEnumerator EnemyMelee (float cooldown)
{
enemyMelee.SetActive (true);
yield return new WaitForSeconds (cooldown);
StopCoroutine (EnemyMelee (cooldown));
}
}
I could probably get rid of this Mover script and incorporate it into the others.
public class EnemyMover : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D enemy_rb;
public GameObject territory;
private EnemyController enemyAI;
private EnemyTerritory enemyAggro;
void Awake ()
{
enemy_rb = GetComponent<Rigidbody2D>();
enemyAI = GetComponent<EnemyController>();
enemyAggro = territory.GetComponent<EnemyTerritory>();
}
void Update ()
{
if (enemyAggro.encroach == false)
{
enemyAI.Rest ();
}
if (enemyAggro.encroach == true)
{
enemyAI.MoveEnemy (moveSpeed,enemy_rb);
}
}
}
Last one is a check if the player has entered a Box Collider attached to child game object of the enemy. This is supposed to activate the movement.
public class EnemyTerritory : MonoBehaviour
{
public bool encroach;
void Start ()
{
encroach = false;
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
encroach = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.tag == "Player")
{
encroach = false;
}
}
}
Answer by Effrum_Hodwaler · May 28, 2019 at 02:34 PM
As for the moving within the range, my best guess would be (in pseudocode)`if (target distance is less than min range) {move back from opponent} else if (target distance just right [greater than min range AND less than max]) {do thing that you're supposed to do at the right distance} else if (target distance greater than max range) {move towards opponent}`
so basically three if statements to handle the 3 conditions of it being too close, too far, or just right.
Follow this Question
Related Questions
Help with falling enemy Unity 3D 0 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer
Hello, i am new to Unity and would like help making a player mechanic. 0 Answers
how to allow the key to only open 1 door rather than all of them? 0 Answers
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer