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 chris102209 · May 06, 2018 at 07:06 PM · movement script

moving cubes consistantly random

is there a code that I can apply to multiple cubes to move in random directions without stopping?

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 Captain_Pineapple · May 06, 2018 at 07:58 PM

This code will generate random movement with random accelleration:

 public Vector3 movementSpeed;
     public Vector3 acceleration;
     public float maxSpeed = 1.0f;
     public float maxAccelleration = 0.1f;
     public void Start()
     {
         movementSpeed = new Vector3 (Random.Range (-maxSpeed, maxSpeed), Random.Range (-maxSpeed, maxSpeed), Random.Range (-maxSpeed, maxSpeed));
         Vector3.ClampMagnitude (movementSpeed, maxSpeed);
         acceleration = Vector3.zero;
     }
 
     public void Update()
     {
         acceleration = new Vector3 (Random.Range (-maxAccelleration, maxAccelleration), Random.Range (-maxAccelleration, maxAccelleration), Random.Range (-maxAccelleration, maxAccelleration));
         Vector3.ClampMagnitude (movementSpeed, maxAccelleration);
         movementSpeed += acceleration;
         transform.position = Vector3.MoveTowards (transform.position, transform.position + movementSpeed, maxSpeed * Time.deltaTime);
     }

Note though that if you really want to exclude the possibility of the cubes stopping you might want to include a statement that checks if the movementSpeed Vector falls below a certain value. If it does simply multiply it by some value until it reaches the desired length again.

Hope this helps.

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 chris102209 · May 06, 2018 at 08:19 PM 0
Share

thank you it works but how to I get it to turn as it moves like go left then right so on..? btw thanks for the code.

avatar image Captain_Pineapple chris102209 · May 07, 2018 at 07:49 AM 0
Share

Can you state your question a bit more precise? Do you want to look only at a 2d-movement? or do you want less "jitterish" behaviour so that they move in more complete turns? Or do you want them to turn in direction of their movement?

avatar image chris102209 Captain_Pineapple · May 08, 2018 at 07:31 PM 0
Share

i want it to be smooth and random updating the direction continuously (like an animal walking on a field) (they also don't fly)

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

82 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Drag object on XZ with perspective 0 Answers

Better Player Movement Code 0 Answers

Moving a player with Rigidbody 2 Answers

Script disables for no reason 1 Answer

How to stop player movements while the player is falling? 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