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 /
avatar image
0
Question by Jdogmaster · Aug 09, 2018 at 03:33 PM · aiai problemsbotsfleeingbot

make an AI Flee

so im working on a game like agar.io, im making an AI that has 3 states (FindFood, Chase, Run) i have little pieces of food that spawn all over the map, and the AI will chase the closest pice of food. Then if a player comes in range, and has less mass than this AI, the AI will chase that player until it either eats it, or it gets out of range. Now i need to make "Run" which will be if a player with more mass comes into range, this player will run away. But the problem, is i need it to be able to run from multiple players at once, cus say that 2 players come in range with more mass, it can't just run in the opposite direction of bot, i would need it to run in-between them, but i don't know how to do this. here is my script

 public float timer;
 private float dist;
 private GameObject currentTarget;
 private GameObject enemy;
 private GameObject currentFood;
 public float Speed = 20f;
 public GameObject[] Targets;
 public GameObject[] Foodtarget;
 public Rigidbody rb;
 public Rigidbody otherRB;


 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 void Update()
 {
     Targets = GameObject.FindGameObjectsWithTag("Player");
     Foodtarget = GameObject.FindGameObjectsWithTag("Food");
     timer += Time.deltaTime;


     {
         currentTarget = ChooseNewEnemy();
         currentFood = ChooseFood();
         StopAllCoroutines();

         if (currentTarget != null && rb.mass > otherRB.mass)
         {
             if (rb.mass > otherRB.mass)
             {
                 StartCoroutine(ChaseTarget(currentTarget));
             }
             Debug.Log("Player");
         }

         else if (currentFood != null)
         {
             StartCoroutine(ChaseFood(currentFood));
             Debug.Log("Food");
         }


     }

 }

 GameObject ChooseNewEnemy()
 {
     GameObject possibleTarget = null;
     float closestDistanceSqr = Mathf.Infinity;
     Vector3 currentPosition = transform.position;
     foreach (GameObject other in Targets)
     {
         Vector3 directionToTarget = other.transform.position - currentPosition;
         dist = Vector3.Distance(transform.position, other.transform.position);
         float dSqrToTarget = directionToTarget.sqrMagnitude;
         if (dist < 15f && dist > 0.5f && dSqrToTarget < closestDistanceSqr)
         {
             otherRB = other.GetComponent<Rigidbody>();
             closestDistanceSqr = dSqrToTarget;
             possibleTarget = other;
         }
     }

     return possibleTarget;
 }

 IEnumerator ChaseTarget(GameObject target)
 {
     if (target == null)
     {
         yield break;
     }

     while (target == currentTarget && rb.mass > otherRB.mass)
     {
         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Speed * Time.deltaTime);

         yield return null;
     }
     while (target == enemy && rb.mass > otherRB.mass)
     {
         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Speed * Time.deltaTime);

         yield return null;
     }
 }

 GameObject ChooseFood()
 {
     GameObject possibleFood = null;
     float closestDistanceSqr = Mathf.Infinity;
     Vector3 currentPosition = transform.position;
     foreach (GameObject other in Foodtarget)
     {
         Vector3 directionToTarget = other.transform.position - currentPosition;
         dist = Vector3.Distance(transform.position, other.transform.position);
         float dSqrToTarget = directionToTarget.sqrMagnitude;
         if (dSqrToTarget < closestDistanceSqr)
         {
             closestDistanceSqr = dSqrToTarget;
             possibleFood = other;
         }
     }

     return possibleFood;
 }

 IEnumerator ChaseFood(GameObject food)
 {
     if (food == null)
     {
         yield break;
     }

     while (food == currentFood)
     {
         transform.position = Vector3.MoveTowards(transform.position, food.transform.position, Speed * Time.deltaTime);

         yield return null;
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 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

Related Questions

AI fleeing from multiple enemies 0 Answers

Resize Array Based on Value 2 Answers

Help with AI script 0 Answers

Properties of Artificail Intellegence in Unity Game Engine 0 Answers

AI Player Jerky Movement - Overcoming Obstacle 0 Answers


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