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 10, 2018 at 07:07 PM · ailoopforeachaddai problems

Making an AI Flee

    private void Flee(List<GameObject> objectsToFlee)
     {
     Vector3 fleeVector = Vector3.zero;

         foreach (GameObject obj in objectsToFlee)
         
         fleeVector += (obj.transform.position + other obj that are in range.transform.position) / number of obj in range
 }

Movetwards(- fleeVector);

} anyone know how to say this? like how could i say all obj in range with more mass than this gameobject added together then divided by the number of those gameobjects. Im making an Ai and I want it to run away from any gameobject with more mass than itself. its not that hard to make it run away if one enemy comes in range, but if 2 or more enemies come in range, i need it to find the best way to run away

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SpeakBeaver · Aug 10, 2018 at 07:15 PM

You can use Physics.OverlapSphere with your object position and a range, and compute your condition by using the array given by this method to know if you need to move away

Hope it helps ! Good Luck for your AI

Comment
Add comment · Show 4 · 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
avatar image Jdogmaster · Aug 10, 2018 at 07:39 PM 0
Share

currently i calculate the distance of each gameobject on the map, and any get to close it will run away but if multiple come in range i just need it to add them together, and divide it by the the number that are in range, the runaway from that point

avatar image SpeakBeaver Jdogmaster · Aug 10, 2018 at 08:32 PM 0
Share

I don't understand what you mean by "add them together and divide it", what do you want to add ? mass , scaling , other properties ?

Because when multiple come in range you have all the information needed if you already the range of each GameObject.

avatar image Jdogmaster SpeakBeaver · Aug 10, 2018 at 08:38 PM 0
Share

so how can i use that info to make it runaway, from multiple gameobjects at the same time?

avatar image SpeakBeaver Jdogmaster · Aug 10, 2018 at 08:39 PM 0
Share

if you want to know the number to divide by, you can create an empty object, who check range for each GameObject (getting using tag for example) and by increment a variable used in the method call who divide objects interested

avatar image
0

Answer by Jdogmaster · Aug 10, 2018 at 08:40 PM

    private void Flee(List<GameObject> objectsToFlee)
     {
     Vector3 fleeVector = Vector3.zero;

         foreach (GameObject obj in objectsToFlee)
         {
         fleeVector += (obj.transform.position - this.transform.position);
         }
     fleeVector /= objectsToFlee.Count;
     Debug.Log("Run");

     MoveTowards(-fleeVector);
     }

     private void MoveTowards(Vector3 target)
     {
         transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed * Time.deltaTime);
     }

i tried this, it kind works but not that great

Comment
Add comment · Show 1 · 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
avatar image SpeakBeaver · Aug 13, 2018 at 02:00 PM 0
Share

Why it's not that great ?

Juste an idea : Ins$$anonymous$$d of working with position, you can work with forces and a RigidBody. Try to compute the vector of the direction where to flee and give to the force this vector until the object arrives to the target. $$anonymous$$aybe it's gonna work better.

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

152 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

Related Questions

foreach loop stops at first item 1 Answer

Ai dodge bullets 3 Answers

Why do the AI get caught on trees? 0 Answers

Help With Add and Remove on a loop 2 Answers

my foreach loop and condition wont work 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