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 Menoites · Nov 06, 2011 at 10:45 AM · 2dmovementrandom

Random Movement in 2D

I would like to find a better way to randomly move 2D GameObjects (Cubes) around randomly - In no specific direction, just within the selected parameters.

 function Update () {
     Invoke("RandomlyMove",randomRate);
     randomX = Random.Range(-2,2);
     randomY = Random.Range(-2,2);
     transform.Translate(Vector3(RandomX,RandomY,0) * moveSpeed * Time.deltaTime);

     if (transform.position.x >= 6.1) {
         transform.position.x = 6.1;
     } else if (transform.position.x <= -6.1) {
         transform.position.x = -6.1;
     }

     if (transform.position.y >= 4.4) {
         transform.position.y = 4.4;
     } else if (transform.position.y <= -4.4) {
         transform.position.y = -4.4;
     }
 }

 function RandomlyMove() {
     RandomX = Random.Range(-2,2);
     RandomY = Random.Range(-2,2);
     CancelInvoke("RandomlyMove");
 }

The objects that I am moving randomly like to bunch up in the corners of the area I have laid out. Does anybody know a better way to set this up?

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

2 Replies

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

Answer by aldonaletto · Nov 06, 2011 at 11:34 AM

You're using RandomX and RandomY to move, but defining a random direction in randomX and randomY, which are different variables. There are also other problems: 1) clamping the object position to the borders make it stick there until a suitable direction is drawn by Random.Range; 2) since you're using integer parameters, Random.Range will return only the values -2, -1, 0 or 1 - there's one chance in four to go to right or up, but two chances in four to go to the opposite direction.

A better approach is to change the direction at random intervals, and revert the direction when the borders are hit. You should also use float arguments, which would force the float version of Random.Range:

 var maxX = 6.1;
 var minX = -6.1;
 var maxY = 4.2;
 var minY = -4.2;
 
 private var tChange: float = 0; // force new direction in the first Update
 private var randomX: float;
 private var randomY: float;
 
 function Update () {
     // change to random direction at random intervals
     if (Time.time >= tChange){
         randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
         randomY = Random.Range(-2.0,2.0); //  between -2.0 and 2.0 is returned
         // set a random interval between 0.5 and 1.5
         tChange = Time.time + Random.Range(0.5,1.5);
     }
     transform.Translate(Vector3(randomX,randomY,0) * moveSpeed * Time.deltaTime);
     // if object reached any border, revert the appropriate direction
     if (transform.position.x >= maxX || transform.position.x <= minX) {
        randomX = -randomX;
     }
     if (transform.position.y >= maxY || transform.position.y <= minY) {
        randomY = -randomY;
     }
     // make sure the position is inside the borders
     transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
     transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);
 }
Comment
Add comment · Show 7 · 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 Menoites · Nov 06, 2011 at 07:24 PM 1
Share

Where does the "Time.timer" come from?

avatar image wolis · Jan 11, 2012 at 08:53 PM 0
Share

I used Time.time that takes the current time, and it works excellently! I think that is what was intended! Thanks aldonaletto!

avatar image aldonaletto · Jan 11, 2012 at 09:34 PM 1
Share

Oops! It should be Time.time, as @vvolis said! Answer edited to fix this asha$$anonymous$$g error... Thanks @vvolis for infor$$anonymous$$g this!

avatar image Pauls · Jul 23, 2013 at 08:36 PM 0
Share

Hi @aldonaletto, I tried your code, I would like to use it in a restricted area, so I added "randomZ = -randomZ;" and "inverseDoneZ = true;" when the IA goes beyond the limits, (and inverseDoneZ = false when it comes back inside the area, the same for the X) but the problem is the IA is trembling all the time. Would you know how to avoid this? Thanks a lot

avatar image aldonaletto · Jul 24, 2013 at 02:15 AM 1
Share

Don't know if this is causing your problem, but the answer above was mutilated due to changes in UA formatting. It's fixed now.

Show more comments
avatar image
0

Answer by Catlard · Nov 06, 2011 at 10:56 AM

Perhaps you could have each cube choose an anchor point to start at randomly--and then have them move based around that, but never be allowed to move away from it twice. That way you could keep them from bunching up.

Also, you may want to allow them to go in the opposite direction if they hit a wall...

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

7 People are following this question.

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

Related Questions

Random Movement : 2d 1 Answer

Unity 2D Random Movement 1 Answer

Top-down movement in 2D 1 Answer

A node in a childnode? 1 Answer

SpriteManager 2 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