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 vittu1994 · Apr 18, 2017 at 09:03 AM · instantiatevelocityspawnfloatproperties

rate of spawn based on speed of object

Hi i have a object that moves around in the scene with a velocity value. This object moves around and slows down over time, decreasing the velocity of its rigidbody. This object can instantiate objects, you can say its dropping things as its moving around. The spawn rate should function like a trail behind the object, spawning objects at an even rate even though the speed of the object varies. For example, when the object starts moving its really fast meaning that the spawn rate should be fast aswell. As the object slows down the spawn rate should slow down aswell.

I worked and tried many different solutions for this. I first tried doing a IEnumerator that is looped with a while loop, the spawn rate is indicated by the float value of WaitForSeconds. I also tried using InvokeRepeating where you can use a normal void function but both methods doesn't seem to cut it for me.

The most important part is that i want to have a float variable that indicates the spawn rate of objects and it should also be flexible. I tried creating a float property where its value should be affected by the objects velocity value.

     private float _netSpawnRate;
 public float netSpawnRate
 {
     get
     {
         return _netSpawnRate;
     }
     set
     {
         value = 1 - boatObj.GetComponent<Rigidbody2D>().velocity.magnitude;
         _netSpawnRate = value;
     }
 }

This is the float property i tried doing. Im not really familiar with them so i believe im doing something wrong here. Once i can get a flexible float variable that changes depending on the objects velocity, i believe i'm set to try and fix this on my own.

I appreciate any type of help! Have a nice day

EDIT: Im considering using an IEnumerator with a while loop where the iteration is based on WaitForSeconds. In this case my float should increase while the velocity of my object decreases, because if the float value is larger then the iteration will be slower.

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 Masterio · Apr 18, 2017 at 07:22 PM

 public boatObj;
 public float spawnInterval = 1f;    // default spawn interval [increase or decrease it if needed]    
 
 private float _t = 0f;                // next spawn time
 private Rigidbody2D _rb;            // handle [dont use GetComponent in update methods]
 
 void Start()
 {
     _rb = boatObj.GetComponent<Rigidbody2D>();
 }
 
 void Update()
 {
     if(_t < spawnInterval)
         _t += Time.deltaTime * _rb.velocity.magnitude;    // greater magnitude increase spawn speed
     else
     {
         _t = 0f;
         
         // spawn object:
         
     }
Comment
Add comment · 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

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

79 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

Related Questions

Give object velocity based on spawn object rotation? 2 Answers

instantiating "debris" with inherited velocity 1 Answer

I want to respawn the food everytime the snake collides with it 0 Answers

Instantiate over time? 1 Answer

How do I control where enemies spawn? 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