Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MaxPlayer2011 · May 03, 2021 at 01:03 AM · random

problem with ai

So I have a WIP script that is a simple AI that follows the player when sees it, and then when she doesn't chase you she will walk around randomly. The problem is that I created public transform[] aiWanderPoints and now I don't know how to choose a random element. Here's my script:

using UnityEngine; using UnityEngine.AI;

public class AI : MonoBehaviour { public Transform Player; private NavMeshAgent agent; public Transform[] aiWanderPoints;

 void Start()
 {
     agent = GetComponent<NavMeshAgent>();
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.CompareTag("Player"))
     {
         agent.SetDestination(Player.position);
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.CompareTag("Player"))
     {
         agent.SetDestination(aiWanderPoints[].position);
     }
 }

}

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fariborzzn · May 03, 2021 at 01:25 AM

Hey @MaxPlayer2011 as I can see you have a array of points that AI can reach out when player is not in trigger zone . what you need is getting an random element from this array . so change your OnTriggerExit to this :

 void OnTriggerExit(Collider other)
  {
      if (other.CompareTag("Player"))
      {
          agent.SetDestination(aiWanderPoints[Random.Range(0, aiWanderPoints.Count)].position);
      }
  }

Random.Range mthod will generate a random number for you .. let me know if your problem was solved or you have more question Best regards Fariborz

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 MaxPlayer2011 · May 03, 2021 at 06:05 PM 0
Share

I'm getting an error:

AI.cs(27,80): error CS1061: 'Transform[]' does not contain a definition for 'Count' and no accessible extension method 'Count' accepting a first argument of type 'Transform[]' could be found (are you missing a using directive or an assembly reference?)

avatar image The-Peaceful MaxPlayer2011 · May 03, 2021 at 08:37 PM 1
Share

Yes, there is no method 'Count' for an array :D Use 'Length' instead.

Here @Fariborzzn 's code changed for using an array instead of a List:

 void OnTriggerExit(Collider other)
   {
       if (other.CompareTag("Player"))
       {
           agent.SetDestination(aiWanderPoints[Random.Range(0, aiWanderPoints.Length)].position);
       }
   }


That should fix your error :P

avatar image Fariborzzn The-Peaceful · May 06, 2021 at 07:48 AM 0
Share

:D Remember array have length not count Remember array have length not count Remember array have length not count Remember array have length not count

avatar image MaxPlayer2011 · May 05, 2021 at 05:39 PM 0
Share

thanks! it worked!

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

124 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

Related Questions

How to move object at random direction,that changes after some amount of time? 2 Answers

Randomly spawn a trigger area 1 Answer

Help with Generating Random Tiles 1 Answer

Random time interval 1 Answer

two Random.Range returning similar values 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