• 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 Matf.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 minSpawnRate and what is its value?

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

minSpawnRate is the minimum time for the WaitForSeconds, for your project it would be minimum 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(minSpawnRate + (1 - mainObj.GetComponent<Rigidbody2D>().velocity.magnitude / maxVelocityMagnitude));


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 maxVelocityMagnitude = 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Why is this loop infinite? 1 Answer

Modify rotation based on passing rigidbody's velocity and direction? 1 Answer

Make rigidbody slower not stop after hit kinematic rigidbody 1 Answer

checking angular magnitude 1 Answer

void OnCollisionEnter(Collider other) { if (Rigidbody.velocity.magnitude > 5) {...} - In "Rigidbody.velocity.magnitude" asks to give link to the object??? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges