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
1
Question by $$anonymous$$ · Nov 02, 2013 at 06:25 PM · movementrandomenemyarea

Random Movement of Enemy within a defined area

Hello!

I have some free roaming code for my enemy that works; the enemy walks around freely within the roamRadius. The problem is if you leave the enemy for a couple of minutes, he can have moved away really far from his original starting position.

I would like to set some boundaries on the enemy so he only moves within certain coordinates of the terrain and this is what I have today;

 void FreeRoam()
 {
     {
         Vector3 randomDirection = Random.insideUnitSphere * roamRadius;
         randomDirection += transform.position;
         NavMeshHit hit;
         NavMesh.SamplePosition(randomDirection, out hit, roamRadius, 1);
         Vector3 finalPosition = hit.position;        
         _nav.destination = finalPosition;
     }
 }


Anyone got any ideas how this could best be solved? I'm guessing the easiest way is perhaps to have a parenting game object with a Box Collider with Trigger and then using that as a reference point as to where the enemy can move inside?

Comment
Add comment · Show 4
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 Cherno · Nov 02, 2013 at 07:14 PM 0
Share

I coded a similar behavior, with a boolean that lets me decide wehter the character may roam around freeley and end up god knows where, or be constrained to a certain area around his starting point.

Basically, when you have chosen a random Vector3 for the character to move towards, you add another check to see if the Vector3.Distance to the coordinates he starts from at game start (which you of course would have to save in a seperate variable in Start()) is equal to or less than a "stray radius" you want.

avatar image Freaking-Pingo · Nov 02, 2013 at 07:21 PM 0
Share

@Cherno I was about to write down the same solution, but this idea is not very delicate, because the script have to keep recalculate a new position and measure a new distance until it finds a valid point. This is perhaps not such a big issue for $$anonymous$$or games with few enemies, but if we are discussing large scale games, this is something that must be taken into account.

avatar image Cherno · Nov 02, 2013 at 09:37 PM 0
Share

The smaller the distance of each random move action gets, the less iteration has to take place.

avatar image Freaking-Pingo · Nov 02, 2013 at 09:59 PM 0
Share

A better solution is no "Random" chance of finding a proper position.

2 Replies

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

Answer by Freaking-Pingo · Nov 02, 2013 at 07:19 PM

My best bet would be to first create a Vector3 that defines it start position, perhaps before it begins to roam. Then, whenever you create a new finalPosition you make sure the start position and finalPosition aren't exceeding some sort of distance threshold.

As an example you could basically just take this startPosition and replace it with transform.position so your code would look a little like this:

 Vector3 startPosition;
 
 void Awake()
 {
    startPosition = GetStartingPosition();
 }
 
 void FreeRoam()
 {
     {
        Vector3 randomDirection = Random.insideUnitSphere * roamRadius;
        randomDirection += startPosition;
        NavMeshHit hit;
        NavMesh.SamplePosition(randomDirection, out hit, roamRadius, 1);
        Vector3 finalPosition = hit.position;     
        _nav.destination = finalPosition;
     }
 }

In this way, the new position is located in respect to the starPosition.

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 $$anonymous$$ · Nov 02, 2013 at 08:30 PM 0
Share

Thanks a lot, this worked out!

avatar image
0

Answer by Zaeran · Nov 02, 2013 at 07:19 PM

You won't necessarily need a box collider. You can use a cube, and use 'renderer.bounds.Contains()' to determine if the next position is inside the area. If not, select a new position or rotate away.

ie.

 if(!renderer.bounds.Contains(randomDirection + transform.position)){
         randomDirection = -randomDirection; // if next point outside boundary, do a 180
     }
     randomDirection += transform.position;
 
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

16 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

Related Questions

Enemy moves randomly within a area. 1 Answer

2D Random AI 1 Answer

random direction of enemy when collide whit wall 1 Answer

Random Object Movement 0 Answers

Enemy following Player on uneven surface 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