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 OtsegoDoom · Apr 25, 2014 at 04:37 PM · vector3randomrandom.rangevector3.position

Random.Range Vector3

I'm wondering if there's a simple way to generate a random Vector3 given maximum and minimum Vector3's.

So far what I've been doing works, but it seems clunky. Is there a better way?

 var minPosition : Vector3;
 var maxPosition : Vector3;
 var randomPosition = Vector3(Random.Range(minPosition.x, maxPosition.x), Random.Range(minPosition.y, maxPosition.y), Random.Range(minPosition.z, maxPosition.z) );

Ideally I'd like to do something like:

 var minPosition : Vector3;
 var maxPosition : Vector3;
 var randomPosition : Vector3 = Random.Range(minPosition, maxPosition);

Is there a better way to find a random value between two Vector3's?

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 MikeNewall · Apr 25, 2014 at 04:52 PM 0
Share
 Random.onUnitSphere * Random.Range($$anonymous$$Distance, maxDistance);

That Would give you a random position between a $$anonymous$$ and max distance.

avatar image robertbu · Apr 25, 2014 at 05:09 PM 1
Share

@$$anonymous$$ikeNewall - your code was my first quick thought. But if you look at his code, it generates a set of random values within an axes aligned rectangular box at an arbitrary position. Your code generates a shell of some thickness with Vector3.zero as the origin.

avatar image robertbu · Apr 25, 2014 at 05:18 PM 0
Share

If you wanted a spherical collection of points around some fixed position, you could do:

var randomPosition = center + Random.insideUnitSphere * maxDist;

avatar image MikeNewall · Apr 25, 2014 at 05:26 PM 0
Share

@roertbu I should read things properly :p Also I forgot about

 Random.insideUnitSphere * radius;  

which would give the same result as his code but with spherical bounds ins$$anonymous$$d of rectangular.

2 Replies

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

Answer by wibble82 · Apr 25, 2014 at 05:18 PM

Your code effectively gives you a random position within a 'box', who's "min" corner is at minPosition and "max" corner is at maxPosition. There's no handy unity function to return you a random vector within a box unfortunately, however you could of course write your own little utility function to do it. Maybe create a class somewhere called 'RandomUtils', with a static function in called 'RandomVectorInBox'.

The Random.onUnitSphere, or Random.insideUnitSphere functions give you a random vector either on the surface of, or inside, a sphere of radius 1. Even if you then multiple the result by a random number you won't end up with an even spread and they wouldn't match the bounds of your box either. Hence I wouldn't recommend doing something with them.

So the simple answer is no - there isn't a nicer way of doing it! But you could write your own little utility function to do it to make your code a little cleaner.

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 OtsegoDoom · Apr 25, 2014 at 05:30 PM 0
Share

@wibble82, I like your explanation. I never really thought about the $$anonymous$$ and max for$$anonymous$$g a box. Too bad there's no Unity function for this. I'll continue doing what I've been doing for now. I'll probably end up writing a utility function like you suggested since this seems to be something I do fairly often.

avatar image
5

Answer by rhodnius · Jul 19, 2019 at 09:12 PM

Here is the code if someone is needing it:

 using UnityEngine;
 
 public static class MathUtilities
 {
     public static void Random(this ref Vector3 myVector, Vector3 min, Vector3 max)
     {
         myVector = new Vector3(UnityEngine.Random.Range(min.x, max.x), UnityEngine.Random.Range(min.y, max.y), UnityEngine.Random.Range(min.z, max.z));
     }
 }

You can use it just by having any vector field and calling from it Random(minVector, maxVector)

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

24 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

Related Questions

Random spawn script not working correctly 3 Answers

Using Vector3 in ViewportToWOrldPoint 1 Answer

Random position of an object spawned from a spawn point? 0 Answers

How are vector3s instantiated? 2 Answers

,How to store multiple Vector3s inside of a single variable every frame? 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