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 JustAndy · Sep 28, 2018 at 04:40 PM · musicspawning problemsrhythm

Music and spawning times not synchronized

Hello there, I'm making a 2d mobile rhythm game, in which you are this player in the middle of the screen that must defeat enemies spawning from the right and left of the screen on the beat of a song.

something like this: https://youtu.be/LAgG6Xn6dv4

The problem is that when I transition from the play menu scene to the game scene and even when I build the game to my mobile device, there is a slight delay between the spawning times and the music beat.

something like this: https://youtu.be/eRF2EP1b-Ig

My question is: How do I make sure that the song and spawning times remain synchronized when transitioning from one scene to the game scene?

This is one of my first posts on Unity Answers, so if I did any mistakes in writing this, please forgive me and tell me how to do it better in the future.

These are the scripts that I use:

EnemiesManager.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemiesManager : MonoBehaviour
 {
     public Transform leftSpawnPoint;
     public Transform rightSpawnPoint;
     public GameObject player;
     public Animator playerAnimator;
     public GameObject[] enemies;
 
     public LevelScriptable level;
     public InputQueue queue;
 
     int spawnIndex;
     float levelTime;
     float levelStartTime;
 
     private Vector3 left, right;
 
     List<Enemy> spawnedEnemies;
 
     void Start ()
     {
         levelStartTime = Time.fixedTime;
         levelTime = Time.fixedTime;
         spawnIndex = 0;
         spawnedEnemies = new List<Enemy>();
         left = new Vector3(-1, 1, 1);
         right = new Vector3(1, 1, 1);
     }
     
 
     void SpawnEnemy(LevelScriptable.Enemy enemy)
     {
         Vector3 spawnPosition;
         if (enemy.zone == LevelScriptable.Zone.Left)
             spawnPosition = leftSpawnPoint.position;
         else
             spawnPosition = rightSpawnPoint.position;
 
         GameObject newEnemy = Instantiate(enemies[(int)enemy.type], spawnPosition, Quaternion.identity);
         newEnemy.GetComponent<Enemy>().Initialize(enemy.zone, enemy.type, enemy.speed, this);
         spawnedEnemies.Add(newEnemy.GetComponent<Enemy>());
     }
 
     private void FixedUpdate()
     {
         if (spawnIndex >= level.enemies.Count)
             return;
 
         if (level.enemies[spawnIndex].time <= levelTime)
         {
             SpawnEnemy(level.enemies[spawnIndex]);
             spawnIndex++;
         }
         levelTime = Time.fixedTime - levelStartTime;
     }
 
     private void Update()
     {
         while(queue.inputs.Count != 0)
         {
             GestureDetector.TouchInfo input = queue.inputs[0];
             if ((int)input.zone == 0)
                 player.transform.localScale = left;
             else player.transform.localScale = right;
 
             if ((int)input.type == 0 || (int)input.type == 2)
                 playerAnimator.Play("Tap");
             else if ((int)input.type == 1)
                 playerAnimator.Play("Swipe");
 
                 for (int i = spawnedEnemies.Count - 1; i >= 0; i--)
             {
                 Enemy curEnemy = spawnedEnemies[i].GetComponent<Enemy>();
                 if ((int)curEnemy.zone == (int)input.zone && (int)curEnemy.type == (int)input.type)
                 {
                     if (curEnemy.isKillable)
                     {
                         KillEnemy(curEnemy);
                         break;
                     }
                 }
             }
             queue.inputs.RemoveAt(0);
         }
     }
 
     public void KillEnemy(Enemy enemy)
     {
         spawnedEnemies.Remove(enemy);
         Destroy(enemy.gameObject);
     }
 }

AudioManager.cs

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     [RequireComponent(typeof(AudioSource))]
     
     
     public class AudioManager : MonoBehaviour
     {
         public AudioSource audio;
     
     
         void Start()
         {
             audio = GetComponent<AudioSource>();
             audio.PlayDelayed(0);
             Debug.Log("started");
         }
     
     }
 

SceneLoader.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class SceneLoader : MonoBehaviour
 {
 
     private bool loadScene = false;
 
     [SerializeField]
     private int scene;
     [SerializeField]
     private Text loadingText;
 
     public void Play()
     {      
         if (loadScene == false)
         {
 
             
             loadScene = true;
 
             loadingText.text = "Loading...";
 
             // ...and start a coroutine that will load the desired scene.
             StartCoroutine(LoadNewScene());
 
         }
     }
 
 
     void Update()
     { 
 
         // If the new scene has started loading...
         if (loadScene == true)
         {
 
             // ...then pulse the transparency of the loading text to let the player know that the computer is still working.
             loadingText.color = new Color(loadingText.color.r, loadingText.color.g, loadingText.color.b, Mathf.PingPong(Time.time, 1));
 
         }
 
     }
 
     IEnumerator LoadNewScene()
     {
 
         yield return new WaitForSeconds(0);
 
         // Start an asynchronous operation to load the scene that was passed to the LoadNewScene coroutine.
         SceneManager.LoadSceneAsync(scene);
     }
 
 }



Thanks for the help, Andy

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 digipaul · Sep 29, 2018 at 08:51 AM

Unitys AudioSource thing has a time field which tells you how long the sound has been playing in seconds. Maybe you should use that to synchronize the enemies spawning.

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 JustAndy · Sep 29, 2018 at 03:57 PM 0
Share

So the spawning times should relate to the song playing time, and not to the scene playing time?

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

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

Related Questions

C# Fourier transform rhytm game implementation? 1 Answer

Calculating rhythm of any music? 3 Answers

check for click in rhythm game 0 Answers

Rhythm Game - Using Midi Notes to trigger a coloured note on screen. 1 Answer

GameObjects not syncing with music 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