Quantcast
Channel: Questions in topic: "side-scrolling"
Viewing all articles
Browse latest Browse all 99

Enemy AI problems

$
0
0
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(); // target = GameObject.FindWithTag ("Player"); // target_rb = target.GetComponent(); // 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(); enemyAI = GetComponent(); enemyAggro = territory.GetComponent(); } 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; } } }

Viewing all articles
Browse latest Browse all 99

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>