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 /
  • Help Room /
avatar image
0
Question by iBenParry · Oct 29, 2016 at 03:52 PM · movementaiai problems

Making the AI move randomly around the player's last known location

I've created an AI that uses a state machine by following a tutorial on Unity's YouTube channel and there's a section I wish to change to give it a more authentic feel. Here you can see how the AI moves around. Firstly, it's in a wander state where it goes to waypoints back and fourth and eventually when it spots the player, it will chase them. Now when the player breaks line of sight, the current code makes the AI stand on the spot and rotate for a few seconds to try and find the player and if not, it'll go back to wandering and if it does, it'll chase them all over again.

However, I feel like it'd look more real and authentic if the AI randomly moved around the area the player was last seen in. The AI already walks to the last known location already, it's just setting up a circular radius and then allowing them to walk around in it, but I'm not quite sure how to do that.

 private void Search()
 {
     searchTimer += Time.deltaTime;
     followTimer += Time.deltaTime;
     
     if (followTimer >= enemy.followingDuration)
     {
         enemy.navMeshAgent.destination = enemy.chaseTarget.position;
         enemy.navMeshAgent.Stop();
     }

     enemy.transform.Rotate(0, enemy.searchingTurnSpeed * Time.deltaTime, 0); //Rotates the enemy on the Y axis
     enemy.meshRendererFlag.material.color = Color.white;

     if (searchTimer >= enemy.searchingDuration)
     {
         InPatrolState(); //After a few seconds of searching, the enemy will stop rotating and will go back to patrolling
     }
 }

This here is the function used for when the AI searches for the player and I want to be able to amend it to do what I wish to do with it. If anyone could help me out, I'd extremely appreciate it, thanks. :)

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

Answer by OutOfRam · Oct 29, 2016 at 05:05 PM

Hi there,

I would personally start by setting a variable for wander radius, then use that and set a random position within that radius over and over again. try the following;

 IEnumerator Search() {
     while(true) { // This loops the behaviour untill you teminate manually
         for (float i = 1; i < 10; 1 += 1) { //will loop 10 times, set it to the number of waypoints you want
             searchPos[i] = Random.insideUnitCircle * searchRadius; //gives you coordinates at random
         }
         //SET NAVE MESH AGENT DESTINATION TO SEARCHPOS[variable]
         //WHEN HE GETS THERE INCREASE variable AND DO IT AGAIN
          if (vaiable is > searchPos.length)
         {
             //Run the patrol behaviour again
             break;
         }
         else if (Player is found)
         {
             //Run follow behaviour
             break;
         }
         yield return new WaitForSeconds(.5f)
     }

I can't do all the work for you, so you'll need to fill in some areas on your own. If you still need help or if i made a mistake somewhere(this was all freehand, it happens) leave a comment and I'll try to help some more.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Enemy AI, when player is not visible i want the Enemy to chase the player for a few more seconds and then stop. 0 Answers

Ai sprite animation 8 directional movement 0 Answers

NPC move and find 0 Answers

Disable a Function when another and active (javascript AI) 1 Answer

Need some help programming basic AI movement 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