Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gamedev8 · Feb 12, 2015 at 12:39 AM · c#gamedelayrhythm

How do I delay instanciated objects randomly to ensure they don't spawn at the same time? (C#)

Hello, I am making a Rhythm game within Unity; and in this, I have 3 spawners, each spawning beats which can be removed once they enter a trigger and the player presses a button. However, now I have them actually spawning to music I have bizarre issues where all 3 spawners either A) Spawn at the same time, and stop spawning at all 30 seconds or so in, and B) Spawn far too many objects at once, causing blockages and what not, balls spiralling all over the screen.

So my question to this is A) How would I go about delaying these spawners randomly, or B) Is there a better fix for this than that?

Note; I do have a random delay but it doesn't work in this version, as changing the values will only cause it to spawn far too many spheres/beats.

 using UnityEngine;
 using System.Collections;
 
 
 public class beat : MonoBehaviour {
     
     public Transform target;
     public int minRange;
     public bool follow;
     private float speed;
     public Rigidbody projectile;
     private float cooldown = 0.5f;
     public int detail = 500;
     public float amplitude = 0.1f;
     public float minValue = 1.0f;
 
 
     
     void Start () {
 
     }
     
     
 
     
     
     void Update(){
         cooldown -= Time.deltaTime;
         float[] info = new float[detail];
         AudioListener.GetOutputData(info, 0); 
         float packagedData = 0.0f;
         
         for(int x = 0; x < info.Length; x++)
         {
             packagedData += System.Math.Abs(info[x]);   
         }
         
         if (packagedData/info.Length > 0.5f) {
             cooldown += Random.Range(1,3);
             Rigidbody clone;
             clone = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody; //This seems to create the clone
             clone.velocity = transform.TransformDirection(Vector3.forward * 40); //This details the velocity of the projectile
 
         
             }
             
         }
 
             
         }
     


Cheers!

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

Answer by Alanisaac · Feb 12, 2015 at 01:07 AM

Cool stuff! I haven't worked on a rhythm game before, but I think I can see how this is working. Let's break down the if statement that controls whether an object is spawned or not:

 if (packagedData/info.Length > 0.5f) {...}

The way your class is set up, the length of the info array, (`info.Length`) is always going to be the same. You instantiate this array in your update() method as info = new float[detail];, effectively giving it a size of detail = 500 from the field in your class.

packagedData comes from the summation of the absolute value of the info array. I can only guess then, that you're summing the magnitude of each frame, dividing by the length, and getting an average amplitude of your sound for 500 "frames" where those frames are determined by whatever the data rate is of the AudioListener (not sure how the data collection works). But if each 500 frames of your sound have an average above 0.5, I can see how you'd start spawning like crazy, and conversely, how lulls would create empty space.

Without knowing what the specific goals of your beat-spawning logic are, I don't know that I can answer your question fully. However, I can say that if your issue is just that you're spawning too often, I'd add in that cooldown variable to your if statement. Something like:

 if (packagedData/info.Length > 0.5f && cooldown < 0)

This should effectively limit the number of times you can spawn beats in a row.

Comment
Add comment · Show 1 · 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 gamedev8 · Feb 18, 2015 at 09:19 AM 0
Share

Thanks! Seems to help out, but do you have any ideas how I could make it spawn quicker? At the moment it spawns fine, but they're really far apart. Just a case of changing values you reckon?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rhythm Game Out of Sync (C#) 1 Answer

Multiple Cars not working 1 Answer

Is there something more reliable than simple Triggers for something like a Rhythm Game? 1 Answer

Distribute terrain in zones 3 Answers

LineRenderer static movement 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