Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Keenin · Jul 19, 2011 at 12:01 AM · 2dmovementrandombeginner

How to create random movement in 2d

I'm trying to make a very basic circle move randomly within a set of specified parameters in 2d. I've found ways to move my circle, but I'm not quite sure how to set specific boundaries for my circle to stay within. Plus I'm not quite sure how to make the object continue to move in a random direction. I've tried using vector3.lerp, but I'm not quite sure if it's right, and if it is, what values to put into where. Suggestions or example code would be greatly appreciated. Thanks.

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
1

Answer by YikYikHeiHei · Jul 19, 2011 at 12:38 AM

This script can random move---

 enum Type2D {XY,XZ,YZ}
 var Type2D : Type2D = Type2D.XY;

 var yourMovementObject : Transform;
 var randomRate = 1.0;
 var moveSpeed = 1.0;
 private var RandomX : int;
 private var RandomY : int;
 private var RandomZ : int;

 function Update ()
 {
     Invoke("RandomlyMove",randomRate);
     //////////////////////////////////////////////x,y,z/////////////////////////////////////////////////////////////////////////////
     switch (Type2D)
     {
     case Type2D.XY:
         yourMovementObject.Translate(Vector3(RandomX,RandomY,0) * moveSpeed * Time.deltaTime);
         break;
     case Type2D.XZ:
         yourMovementObject.Translate(Vector3(RandomX,0,RandomZ) * moveSpeed * Time.deltaTime,Space.World);
         break;
     case Type2D.YZ:
         yourMovementObject.Translate(Vector3(0,RandomY,RandomZ) * moveSpeed * Time.deltaTime,Space.World);
         break;
     }
 }

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

Hope it can help you!

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 Keenin · Jul 19, 2011 at 12:49 AM 0
Share

Okay, thanks YikYikHeiHei, but when applying this script to my circle it allows the circle to move in any range. How do I change it so that it stays within a set area while doing this? Thanks.

avatar image
1

Answer by YikYikHeiHei · Jul 19, 2011 at 01:42 AM

It can fixed the move range ---

 enum Type2D {XY,XZ,YZ}
 var Type2D : Type2D = Type2D.XY;

 var yourMovementObject : Transform;
 var randomRate = 1.0;
 var moveSpeed = 1.0;

 class MaxPosition
 {
     var UpMaxPositionX = 5;
     var DownMaxPositionX = -5;
 
     var UpMaxPositionY = 5;
     var DownMaxPositionY = -5;
 
     var UpMaxPositionZ = 5;
     var DownMaxPositionZ = -5;
 }

 var MaxPosition : MaxPosition;
 private var RandomX : int;
 private var RandomY : int;
 private var RandomZ : int;

 function Update ()
 {
     Invoke("RandomlyMove",randomRate);
 //////////////////////////////////////////////x,y,z/////////////////////////////////////////////////////////////////////////////
    switch (Type2D)
    {
         case Type2D.XY:
             yourMovementObject.Translate(Vector3(RandomX,RandomY,0) * moveSpeed * Time.deltaTime);
             break;
         case Type2D.XZ:
             yourMovementObject.Translate(Vector3(RandomX,0,RandomZ) * moveSpeed * Time.deltaTime,Space.World);
             break;
         case Type2D.YZ:
             yourMovementObject.Translate(Vector3(0,RandomY,RandomZ) * moveSpeed * Time.deltaTime,Space.World);
             break;
     }
 
     if (yourMovementObject.transform.position.x > MaxPosition.UpMaxPositionX)
         yourMovementObject.transform.position.x = MaxPosition.UpMaxPositionX;
     else if (yourMovementObject.transform.position.x < MaxPosition.DownMaxPositionX)
         yourMovementObject.transform.position.x = MaxPosition.DownMaxPositionX;
     
     if (yourMovementObject.transform.position.y > MaxPosition.UpMaxPositionY)
         yourMovementObject.transform.position.y = MaxPosition.UpMaxPositionY;
     else if (yourMovementObject.transform.position.y < MaxPosition.DownMaxPositionY)
         yourMovementObject.transform.position.y = MaxPosition.DownMaxPositionY;
 
     if (yourMovementObject.transform.position.z > MaxPosition.UpMaxPositionZ)
         yourMovementObject.transform.position.z = MaxPosition.UpMaxPositionZ;
     else if (yourMovementObject.transform.position.z < MaxPosition.DownMaxPositionZ)
         yourMovementObject.transform.position.z = MaxPosition.DownMaxPositionZ;
 }

 function RandomlyMove ()
 {
     RandomX = Random.Range(-2,3);
     RandomY = Random.Range(-2,3);
     RandomZ = Random.Range(-2,3);
     CancelInvoke("RandomlyMove");
 }
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 msmatis · Oct 05, 2011 at 10:07 PM 0
Share

this is very good but I have one small problem every object is return on 0,0,0 coordinates - I place cube on 10,15,0 for example but on start cube is on 0,0,0 How can I fix this? Thank you:)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Random movement? 2 Answers

Random Movement in 2D 2 Answers

2d obstacle moving up and down question 1 Answer

Random movement direction cycle. 2 Answers

How do I make 2 axis movement for a sprite in Unity 2D? 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