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 LtRocky · Nov 24, 2013 at 01:07 PM · movementvector3randomdegreesarc

Get a random point in an arc within a specified min/max distance

I'm programming a gameobject to move across a field towards a waypoint via NavMeshAgent, but wish to make it that so that the gameobject won't move in a straight line towards the point in a single sitting. I'm looking to have it set that it'll only move a certain min/max distance at a time, and within an arc that faces the point, and repeat the process until it reaches the point. I have the interval workings set up, so I only wish for help about getting a new point for each one.

Here's the gist of it: 1: Get two vectors on the field 2: Generate an arc (let's say 90 degrees) from the first vector that faces towards the second vector 3: get a random point within the arc that stays within the limits of a minimum and maximum distance from the first vector (say, 20 and 40, respectively)

C# is preferred, but I have no issues with answers written in Javascript. Any suggestions are appreciated. Thank you!

Here is the code I tried making to achieve this. The problem with this code, above being rather all over the place, is that when too many units running this script are present on the field, Unity suffers a memory error and freezes.

     //Get the vectors of the tank and the target destination,
     Vector3 pos = transform.position;
     Vector3 dest = currentDestination.transform.position;
     float rnd = Random.Range(travelDistMin,travelDistMax);
     //Make a new point that's towards the target destination between the tank's min and max travel distance
     Vector3 newPos = Vector3.MoveTowards(pos, dest, rnd);
                         
     bool found = false;
     while (found == false)
     {
          //Establish a random point within a radius
          Vector3 randomDirection = Random.insideUnitSphere * (travelDistMax);
                             
          //Add the random point's offset to the new position
          randomDirection += newPos;
          //Ensure randomDirection is within a narrow angle from pos and newPos
          if(Vector3.Angle(randomDirection - pos, newPos - pos) < 90)
          {
             //Translate the position onto a valid spot in the NavMesh
             NavMeshHit hit;
             NavMesh.SamplePosition(randomDirection, out hit, 20, 1);
                                 
             //Set the final position to the new position + the offet to an elevation on the NavMesh
             Vector3 finalPosition = hit.position;
                                 
             //destinate the final position, reset the stopping distance to the standard value, and change the AI state
             brain.destination = finalPosition;
             brain.stoppingDistance = standardStoppingDistance;
             currentState = AIStates.Moving;
 
             found = true;
           }
      }

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 robertbu · Nov 24, 2013 at 04:25 PM

Without more info, I have to guess a bit. The code will look something like (untested):

 function GenerateNewVector() : Vector3 {
     var v3 = target.pos - transform.position;
     v3.y = 0.0;
     dist = v3.magnitude;

     if (dist > minDist) {
         v3 = Quaternion.AngleAxis(0.0, Ransom.Range(-maxAngle, maxAngle), 0.0) * v3;
         v3 = v3.normalized * Random.Range(minDist, Mathf.Min(maxDist, dist)); 
     }
     return v3;
 }

Note maxAngle is the maximum angle you want with respect to perfect aim.

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 LtRocky · Nov 24, 2013 at 10:00 PM 0
Share

I added some more info to the question. Hope this helps. Thank you for your contributions!

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

Random direction with Mouse Click... 2 Answers

Arcing and object along the X axis with a random amount 0 Answers

How to get our character controller script to make our player move? 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Trying to understand directional calculations 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