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 Dqimass · Apr 26, 2016 at 05:55 AM · unity 5random2d-platformerspawnrange

how to have object fall using random speed

I have a object falling down slowly which is fine, however, while it is falling, it's also moving left and right. I instantiate this falling object so hopefully when it move left and right, it moves all in random speed. here is my script

 void Start () 
 {
     fall = 2f;
     tempVal = transform.position.x;
     tempPos = transform.position;
 }

 // Update is called once per frame
 void Update ()
 {
     tempPos.x = tempVal + amplitude * Mathf.Sin (speed * Time.time);
     tempPos.x = Random.Range (-4, 4);
     transform.position = tempPos;

     Vector2 position = transform.position;
     tempPos = new Vector2 (position.x, position.y - fall * Time.deltaTime);
     transform.position = position;

Random.Range made my object moving left and right randomly but it's moving too fast. how do i make it move left and right, nice and smooth in random speed

Comment
Add comment · Show 2
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 fabian-mkv · Apr 26, 2016 at 06:34 AM 0
Share

Notice that in line 11. you set tempPos.x, and in line 12. you change it again, completely disregarding what you did in line 11. Also, in line 11. Random.Range will set the position to some random value between -4 and 4 EVERY FRA$$anonymous$$E. So 60 times per second, it will jump around like crazy.

There are a few ways to get "random velocity" in this case.

1) You could attach a RigidBody to your object, which now gives you things like "velocity" so that the physics engine moves the object and "AddForce" so that you can apply force to cause velocity to change smoothly.

2) You could make a new variable, "float theta = 0f;" and "float thetaSpeed = 0f;" and have thetaSpeed change every frame randomly and have do "theta += thetaSpeed Time.deltaTime" and use "tempPos.x = tempVal + amplitude $$anonymous$$athf.Sin(theta);".

Also, when program$$anonymous$$g, try to always give meaningful variable names ins$$anonymous$$d of "tempSomething". For example, it looks like tempVal doesn't change after Start. So call it "startPosX" ins$$anonymous$$d. Or "currentPos" ins$$anonymous$$d of "tempPos".

Good luck.

avatar image Dqimass fabian-mkv · May 03, 2016 at 01:50 AM 0
Share

ahh i see. thanks for the info. i'm still new to scripting and I've been throwing in random script to test things out >.< but no luck in succeeding. Thanks a lot though, i'll give it a shot

0 Replies

· Add your reply
  • Sort: 

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

71 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

Related Questions

Object Spawner acts really badly, 0 Answers

Network: Random spawn 1 Answer

Random.Range changed in Unity 5? 2 Answers

How To Random Range a Float? 2 Answers

two Random.Range returning similar values 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