Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 16, 2020 at 01:26 AM by runemyth0 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by runemyth0 · Jun 25, 2018 at 09:07 PM · scripting problemscripting beginnerenemy aiside-scrolling

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges