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 Ekta-Mehta-D · May 31, 2013 at 05:13 AM · 2dmovementtransformrandom

Random Movement : 2d

Hello Everyone..

I am making 2D space shooter kind of game and in that i have scene with x-y plane.

Here i am translating my enemy object in y direction from top to bottom.. But i want my object to change direction after crossing some random distance means i want my object to move left or right diagonally after some random distance..

But i am not getting the way to perform this behavior as i know how to translate object diagonally..

Code which i used is here :

         whatWayToGoInLife = Vector3.left * amttomove;
         whatWayToGoInLife = whatWayToGoInLife + Vector3.down * amttomove;
         transform.Translate(whatWayToGoInLife , Space.World);

But main problem is after crossing some distance on y axis i want my object to move in random direction..

Pleaze someone give me idea..

Thanks in advance for your support and help..

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

1 Reply

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

Answer by robertbu · May 31, 2013 at 05:35 AM

One way is to use Random.insideUnitCircle. I'm assuming your character is walking on the X/Z plane. Random.insideUniytCircle returns a Vector2, so you will need to covert it to a Vector3 to use as a direction. When you want to change directions, you redefine the movement vector:

 Vector2 v2 = Random.insideUnitCircle;
 direction = new Vector3(v2.x, 0.0f, v2.y);

And you code for movement might look like:

 transform.Translate(direction * speed * Time.deltaTime, Space.World);


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 Ekta-Mehta-D · May 31, 2013 at 06:06 AM 0
Share

hmm i have written code :

 function Update () {
     
     var amttomove : float = currentSpeed * Time.deltaTime;
     if(transform.position.y <= -6.00f)
     {
         //SetPositionandSpeed();
     }
     
     if(transform.position.y <= 1.00f)
     {
         SetRandomPosition();
         transform.Translate(direction * amttomove, Space.World);
     }
     else
     {
         transform.Translate(Vector3.down * amttomove , Space.World);
     }
 }
 
 function SetPositionandSpeed()
 {
     currentSpeed = Random.Range($$anonymous$$Speed , maxSpeed);
     
     x = Random.Range(-3.5f , 3.5f);
     z = -6.72f;
     y = 8.0f;
     transform.position = new Vector3(x , y , z);
     
     if(gameControllerScript.gameStarted)
         scoreControllerScript.$$anonymous$$anageEnimies();
 }
 
 function SetRandomPosition()
 {
     var v2 : Vector2= Random.insideUnitCircle * 2;
     direction = new Vector3(v2.x, v2.y, 0.0f);
 }
avatar image Ekta-Mehta-D · May 31, 2013 at 06:09 AM 0
Share

But this is not correct as function is being called on each frame.. so how do i make my player to move after crossing some y distance ? should i use timer or calculate y distance??

avatar image robertbu · May 31, 2013 at 06:35 AM 1
Share

You could structure it like this:

 private var direction = Vector3.down;
 private var newDirection = false;
 
 function Update () {
  
     var amttomove : float = currentSpeed * Time.deltaTime;
  //   if(transform.position.y <= -6.00f)
  //   {
        //SetPositionandSpeed();
  //   }
  
     if(transform.position.y <= 1.00f && !newDirection)
     {
        SetRandomDirection();
        newDirection = true;
     }
 
     transform.Translate(direction * amttomove, Space.World);
 }
  
 
 function SetRandomDirection()
 {
     var v2 : Vector2= Random.insideUnitCircle * 2;
     direction = new Vector3(v2.x, v2.y, 0.0f);
 }

Note I've changed the name of your function, since this code sets a direction, not a position. Also look how direction is initially set to Vector3.down.

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

13 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

Related Questions

Random Movement in 2D 2 Answers

Move multiple objects relative to another objects movement 1 Answer

Cannot move elements in 2D scene. Help 3 Answers

Stop Moving When Not Touching Screen? 1 Answer

Why does my physics2D script cause the player to randomly stop moving? 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