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 awkwardtodd · Jul 13, 2011 at 01:32 PM · transformrandommovmentexpression

Random Movment using if statments

In this project, I need to make an object move randomly around in 2d without going past my boundaries. I'm trying to do it with only the transform function.

It basically works by having the object move in a certain direction and checking to see if it at a certain position and then it changes direction.

I'm trying to set the conditional to a random number range that the user can set so it seems to flow back and forth randomly.

What ever I try seems to only want to travel in a constant direction like up to the right.

Any help would be wonderful.

// public variables

var xSpeed : float;

var ySpeed : float;

var xSpread : float;

var ySpread : float;

// private variables

private var xBoundry : float = 4;

private var yBoundry : float = 4;

function Update() {

 transform.Translate(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 0);
 
 var positionCube = transform.position;
 

 
 var xLimit = Random.Range( -1 * xSpread , xSpread);
 
 var yLimit = Random.Range( -1 * ySpread , ySpread);
 
 if ( positionCube.x >= Mathf.Abs(xLimit))
 {
 
     transform.Translate(-1 * xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 0);
     
 }
 
 
 

 if (positionCube.y >= Mathf.Abs(yLimit) )
 {
 
     transform.Translate(xSpeed * Time.deltaTime, -1 * ySpeed * Time.deltaTime, 0);

     
 }
     
 

}

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 aldonaletto · Jul 13, 2011 at 02:05 PM

EDITED: You should have a direction variable for each axis and invert it when the limit is reached:

 var dirX = 1; // dir variables defined outside any function
 var dirY = 1;
     
 function Update() {
 
     var positionCube = transform.position;
     var xLimit = Random.Range( -1 * xSpread , xSpread);
     var yLimit = Random.Range( -1 * ySpread , ySpread);
 
     if ( positionCube.x >= Mathf.Abs(xLimit))
     {
         dirX = -dirX;
     }
     if (positionCube.y >= Mathf.Abs(yLimit) )
     {
         dirY = -dirY;
     }
     transform.Translate(xSpeed * dirX * Time.deltaTime, ySpeed * dirY * Time.deltaTime, 0);
 }

This is a bit different from what you had: the limits are defined and checked, and only then the cube is moved.

42

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 aldonaletto · Jul 13, 2011 at 02:41 PM 0
Share

Edited to make it more compatible with the original script.

avatar image
0

Answer by awkwardtodd · Jul 13, 2011 at 03:10 PM

Thank you very much! I worked it out an realized I needed another absolute value function.

Here is the final result, though i might need to change variable names to make it more semantic.

// public variables

var xSpeed : float;

var ySpeed : float;

var xSpread : float;

var ySpread : float;

// private variables

private var xBoundry : float = 5;

private var yBoundry : float = 5;

var dirX = 1; // dir variables defined outside any function var dirY = 1;

function Update() {

 var positionCube = transform.position;
 var xLimit = Random.Range( -1 * xSpread , xSpread);
 var yLimit = Random.Range( -1 * ySpread , ySpread);

 if ( Mathf.Abs(positionCube.x) >= Mathf.Abs(xLimit) || Mathf.Abs(positionCube.x) >= xBoundry)
 {
     dirX = -dirX;
 }
 else if ( Mathf.Abs(positionCube.y) >= Mathf.Abs(yLimit) || Mathf.Abs(positionCube.y) == yBoundry )
 {
     dirY = -dirY;
 }
 transform.Translate(xSpeed * dirX * Time.deltaTime, ySpeed * dirY * Time.deltaTime, 0);

}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Moving right and Left 1 Answer

Random position? 1 Answer

Random Position Inside an object 1 Answer

problem extracting transform from an array 1 Answer

moving random objects to random positions 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