Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Music and Movement synchronization

Discussion in 'Scripting' started by kerrygah, Jan 28, 2017.

  1. kerrygah

    kerrygah

    Joined:
    Aug 31, 2016
    Posts:
    12
    Hello everyone. I have problem with synchronization start music and movement my player om way. On different decices i have different FPS and music and movement player have unsynchronization.
    Move and start MusicPlay im use in FiixedUpdate. And multiplier for move player its step update fixedTime.
    In Init game i set FPS = 30. How i can synchronizate music and move on different divases. tnanks for help
    Code (CSharp):
    1. float fixedDTime = 0.02f;
    2. void Update()
    3.     {
    4.      
    5.         if (!GameController.Instance.isGameStart)
    6.         {
    7.             if(Input.GetMouseButtonDown(0))
    8.             {
    9.                 GameController.Instance.GameStartAction();
    10.             }
    11.             return;
    12.         }
    13.      
    14.         // смена направления движения
    15.         if (Input.GetMouseButtonDown(0) && canClick)
    16.         {
    17.             click = !click;
    18.             if (click)
    19.             {
    20.                 direction = X_Dir.Direction;
    21.             }
    22.             else
    23.             {
    24.                 direction = Z_Dir.Direction;
    25.             }
    26.         }
    27.      
    28.  
    29.     }
    30.  
    31.     private void CustomUpd()
    32.     {
    33.         if (canClick) // обычное движение
    34.         {
    35.             print(fixedDTime * playerSpeed);
    36.             transform.Translate(direction * fixedDTime * playerSpeed);
    37.         }
    38.         else
    39.         { // полет над бездной
    40.             transform.position = Vector3.MoveTowards(transform.position, flyDestination, Time.fixedDeltaTime * playerSpeed*1.38f);
    41.             if (Vector3.Distance(transform.position, flyDestination) == 0)
    42.             {
    43.                 canClick = true;
    44.             }
    45.         }
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void FixedUpdate()
    50.     {
    51.         if (!GameController.Instance.isGameStart) return;
    52.  
    53.         if (Time.time - timeMusic > 1f && !isMusicStart)
    54.         {
    55.             isMusicStart = true;
    56.             GameController.Instance.PlayMusic();
    57.         }
    58.         //print("TimeStart " + timeStart + "====== Time " + Time.time);
    59.         if (timeStart < Time.time)
    60.         {
    61.             timeStart += fixedDTime;
    62.             CustomUpd();
    63.         }
    64.     }
    65.  
    66.  
    67.  
    68.  
     
  2. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    I recommend a coroutine or, if you are using an animation, try animation events. They can play sounds etc at certain points in the animation. Animation events support any method that returns void and has one parameter. Beware that the animation event seems to search the component the Animation component is on for the method you provide. Also as far as I know animation events are heavy on the CPU because they use some kind of weird CPU expensive C# feature to find the method.