Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 vittu1994 · Apr 13, 2017 at 10:13 AM · rigidbodytimevelocitypropertiesmagnitude

increase float when another one decreases

Hey im trying to create a float for example called spawnRate for my IEnumerator. The IEnumerator will under a while loop spawn objects. The function will keep spawning objects and before each spawn it will yield a waitforseconds and the float argument will be the spawnRate variable. There is a main object that moves around that will spawn these objects and i want make use of the rigidbody of this main object, its velocity magnitude float variable to control the spawnRate variable. Simply put, when the velocity magnitude of the main object decreases, i want the spawnRate variable to increase. The goal is for each spawn is a linear rate for when objects spawn so logically the rate should decrease once the velocity decreases.

 IEnumerator SpawnNet()
     {
         while(mainObj.GetComponent<Rigidbody2D>().velocity.magnitude > 1f)
         {
             //code//
             yield return new WaitForSeconds(spawnRate);
         }
     }

Im trying to create a float property for the spawnRate:

 private float _spawnRate;
     public float spawnRate
     {
         get
         {
             return _spawnRate;
         }
         set
         {
                     //alter the value based on: mainObj.GetComponent<Rigidbody2D>().velocity.magnitude//
             //code//
         }
 
     }

In the set i should write the code that alters the value of the spawnRate variable based on the velocity of the main objects rigidbody. But im not sure how to do it. As shown, i need to alter the value of the spawnRate based on the mainObjects rigidbody velocity. If you could help me it would be much 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kossuranta · Apr 13, 2017 at 10:49 AM

Do know the max velocity of the mainObj? Depending how your game works you might be able to just simply do something like this:

 yield return new WaitForSeconds(minSpawnRate + (1 - maxVelocityMagnitude / mainObj.GetComponent<Rigidbody2D>().velocity.magnitude));

At the max speed WaitForSeconds will wait for minSpawnRate and at any lower speeds it will wait for minSpawnRate + 0 to 1.

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 vittu1994 · Apr 13, 2017 at 11:01 AM 0
Share

I have a $$anonymous$$atf.Clamp for the amount of boost you can give the mainObj. The speed is instantiated through AddForce, and the max velocity i can see is at around 47f but its not exact. What is $$anonymous$$SpawnRate and what is its value?

avatar image Kossuranta vittu1994 · Apr 13, 2017 at 11:29 AM 0
Share

$$anonymous$$SpawnRate is the $$anonymous$$imum time for the WaitForSeconds, for your project it would be $$anonymous$$imum spawn rate that is used when the mainObj is moving at full speed. You should give it some value to your liking like you already did have spawnRate.

Actually I just noticed that my code is wrong as it should be

 yield return new WaitForSeconds($$anonymous$$SpawnRate + (1 - mainObj.GetComponent<Rigidbody2D>().velocity.magnitude / maxVelocity$$anonymous$$agnitude));


As you don't have exact max velocity my solution might not be the best way to accomplish this, but if it is generally pretty close to that 47f you said then you could just make maxVelocity$$anonymous$$agnitude = 47f and roll with it.

avatar image vittu1994 Kossuranta · Apr 13, 2017 at 11:48 AM 0
Share

Doesnt seem to do much, the rate is about the same regardless of the velocity of the boat (the objects are pretty crammed at the end since the main obj slows down and the rate is still the same)

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

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Calculating speed with rigidbody.velocity.magnitude 0 Answers

Rigidbody magnitude comparison is not working correctlly? 2 Answers

When is Velocity calculated? 1 Answer

Why is this loop infinite? 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