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 BHS · Mar 14, 2012 at 09:18 PM · inputpositionrandom

Move to random position based off user's input of terrain size?

We need a way for our rain to randomly move around the parameters of the user's terrain size, which is inputted by the user.

How can we make it so that the rain will randomly move across the terrain based on time.

Example (which will be hard to explain): The rain ellipsoid is set at 500x500 and the terrain size is 5000x5000 the rain is moving randomly based off the user's input of the terrain size 5000x5000. Since the rain is 500x500 moving at a few degrees a second it can only cover a parameter of 500x500 at a time. So it can move itself across the terrain over time to 2500x2500 then over more time 1000x1000 ect. So pick a point on the terrain and move towards it.

So we need the rain to move across the x and z axis only within the parameters of the given input. The rain has to move all as one object which makes it confusing for having random positions for 2 axis'.

Here's what we got so far it doesn't work properly. It just rapidly moves all over the terrain like it's glitching, but it is pick random points. Also it always sets the Y axis to 14 for some reason.

For X it's 0 to 5000 For Z it's 0 to 5000

Covering all possible terrain areas.

 var parameter1 = 0;
 var parameter2  = 0;
 var parameter3  = 0;
 var parameter4  = 0;

 parameter1 = Time.time;
 parameter2 = Time.time;
 parameter3 = Time.time;
 parameter4 = Time.time;

  transform.position = Vector3(Random.Range(parameter1, parameter2), 809.3739,  
      Random.Range(parameter3, parameter4));

Comment
Add comment · Show 1
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 BHS · Mar 14, 2012 at 09:28 PM 0
Share

Also it always sets the Y axis to 14 for some reason.

For X it's 0 to 5000 For Z it's 0 to 5000

2 Replies

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

Answer by aldonaletto · Mar 15, 2012 at 12:15 AM

You can create a random point and move to it at constant speed with MoveTowards; when the point is reached, create a new random point and repeat the loop:

var speed: float = 20.0; // rain absolute speed var xMin: float = 0.0; // set the terrain limits var xMax: float = 5000.0; var zMin: float = 0.0; var zMax: float = 5000.0;

private var point: Vector3;

function Start(){ // force a new random point in the first Update: point = transform.position; }

function Update(){ if (transform.position == point){ // if current random point reached... point.x = Random.Range(xMin, xMax); // draw new x, z coordinates, but point.z = Random.Range(zMin, zMax); // don't change the original y } // anyway, move towards the current random point: transform.position = Vector3.MoveTowards(transform.position, point, speed*Time.deltaTime); } MoveTowards(a, b, dist) returns a point at dist distance from a in direction to b; the point returned is clamped to b, thus the rain will never pass this point.
NOTE: If you don't want to rain out of the terrain, subtract the ellipsoid half-size from the limits: if it's 500x500, set xMin=250, xMax=4750, zMin=250, zMax=4750, for instance.

Comment
Add comment · Show 3 · 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 BHS · Mar 15, 2012 at 07:48 PM 0
Share

Thank you for this it does exactly what I need. There is one problem though, it stops after a few position changes. Is there anyway to continually loop this so it never ends?

avatar image aldonaletto · Mar 16, 2012 at 12:50 AM 0
Share

Weird thing! It should not stop - the rain should keep moving to the current random point, and draw a new one whenever the current point was reached.
You've said the rain was changing the Y coordinate to 14 due to some reason. $$anonymous$$aybe some other script is doing this, what could even prevent the rain to reach the current random point: if some other script is setting y to 14 each update or fixed update, the rain may get stuck at the current x,z position, never reaching the desired y.
You could prevent this forcing y to the correct value:

function Update(){
  transform.position.y = point.y; // restore rain y 
  if (transform.position == point){ // if current random point reached...
    ...
avatar image BHS · Mar 16, 2012 at 05:46 AM 0
Share

Actually it I'd work, it can't be attached to another object, but you can attach object to it. Thanks again

avatar image
0

Answer by DaveA · Mar 15, 2012 at 12:09 AM

I'm guessing this is in an Update function?

 parameter1 = Time.time;
 parameter2 = Time.time;
 parameter3 = Time.time;
 parameter4 = Time.time;

That's going to set all 4 parameter variables to the same exact value. So not very random. Why not use something like Random.Range(0f,5000f)? Or Random.value * 5000?

Are you trying to pick random spots for this rain patch to move, and have it move over time? I would look into using Mathf.Lerp (see scripting reference). Pick a new random position every so often (but not every frame!) and set a 'target' position. Then let it Lerp toward that target.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Constant camera shake. 1 Answer

Controlling parent by Inputs 1 Answer

How to change direction without changing speed? 1 Answer

ScreenToWorldPoint fixed height 1 Answer

Instantiate object at touch position problem 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