SYNC YOUR UNITY GAME TO THE BEAT

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

Комментарии • 62

  • @Cyclone1024
    @Cyclone1024 9 месяцев назад +42

    I’m a hardware audio design engineer who designs PCM bus and audio serial interfaces for a living. I was scoffing at this at the beginning, but ended up stealing all the code

    • @b3agz
      @b3agz  9 месяцев назад +6

      I'm pinning this 😂

    • @pureay2700
      @pureay2700 4 месяца назад

      ​@@b3agzIt's been 4 years since your videos first got me into really programming my own projects (my boxes engine) and I'm once again led back to you lmao

  • @prispalos
    @prispalos 11 месяцев назад +22

    This is **the definitive way** to handle beat matching in Unity. You can't even imagine how much you saved me with this video. Cheers!!

    • @b3agz
      @b3agz  11 месяцев назад +2

      Glad it helped!

  • @labyrinthHealth
    @labyrinthHealth 3 месяца назад +4

    I know 99% of people who see this can easily figure it out, but by adding a Slider control to the BeatController script, you can control this in game by setting the pitch of the audio source to the value of the Slider. Great tutorial!!!

  • @itmightbeMishu
    @itmightbeMishu Год назад +7

    I was trying to accomplish the same effect with Coroutines and WaitForSeconds(), but that wasn't perfect (it desynced every now and then). Your solution is much more elegant and useful! Thanks for the tips!!!
    PS: The steps thing is called note length. *2 would be a half note (2 notes per beat), *4 - a quarter note etc. And I guess 0.5 would be "half-time". (I'm not a very good musician, though. So take it with a grain of salt)

  • @trulyazul4162
    @trulyazul4162 4 месяца назад

    Thank you so much for this video! Trying to get audio to sink to a beat has been the bane of my existence across so many engines, and yet this works well and was surprisingly simple to follow!

  • @feli7487
    @feli7487 Год назад +2

    For as long as I can remember I've been curious about how I could synchronize enemy attacks with the music, and this is the most complete I've seen.
    Thank you very much :)

  • @Joey-tb8fh
    @Joey-tb8fh Год назад +2

    This is genuinely brilliant. I've been working on my university project for some time and was getting frustrated with the game being out of sync with my music, especially given the song is fast paved so it's very obvious very quick. This couldn't have been shown to me at a better time!

  • @b3agz
    @b3agz  Год назад +2

    The music used in the video is piece by Johnnie Holiday kindly released as a CC0 track ( freesound.org/people/Johnnie_Holiday/sounds/563651/ ) and the asset pack used is Unity's own free 3D Game Kit ( assetstore.unity.com/packages/templates/tutorials/3d-game-kit-115747?aid=1101l9Tam& )

  • @letenebrae3164
    @letenebrae3164 Год назад +1

    Hifi-rush got me really curius on how music syncing works and this tutorial helped me a lot 😄

  • @Vintagestep
    @Vintagestep 8 месяцев назад +2

    I'm just starting my internship and I'm very new at Unity but I was told to learn it and research on topics I'd like to know. I'd like to work with music and audio, I do some music myself as well, so this was a good start. I did some changes, like setting an offset to the beats so they would align to the down beat for example, added a start delay for songs that are not made to loop. This was a very good starting point for someone that knows some programming and knows a little bit abut music.

    • @saing_rbn5368
      @saing_rbn5368 2 месяца назад

      How did you manage the offset?

    • @Vintagestep
      @Vintagestep 2 месяца назад

      @@saing_rbn5368 Well, tbh the solution is not very elegant because it's proportionally inverse to how we understand beats, but I'll try to explain:
      what we want to modify is when the rhythm trigger/interval happens right? So in that class declare a float, I called mine " private float _phaseOffset = 0f;", you can add the [serializefield] to tweak it in the menus. Then since it's a fixed value, it will move the beat at a fixed rate when it's checked by doing "int currentPulse = Mathf.FloorToInt(pulse + _phaseOffset);" after that you do the check "if (currentPulse != _lastPulse) " and the operation to update the pulse and trigger it in the method "checkforNewInterval".
      The values of the offset have to be set between 0~1, more will just cycle over.

  • @I-OGameDev
    @I-OGameDev 5 месяцев назад +1

    Bro explained the math so well I went straight in and built it first try

  • @kylopootis6489
    @kylopootis6489 Год назад +4

    i was so stuck using fixedUpdate, altering timesteps, time.time multiplied by blah blah, using different ways but the game kept getting out of sync after a few minutes. thank you so much

  • @seaweeddol8171
    @seaweeddol8171 7 месяцев назад +1

    Great video! I made a small rhythm game for a game jam where you have to interact on the beat, but it wasn't synced properly. This tutorial helped me get it working better.

    • @b3agz
      @b3agz  7 месяцев назад

      Awesome! Glad it helped.

    • @marionateixido1789
      @marionateixido1789 6 месяцев назад

      Hi! can you share or explain how you coded the part of interacting on the beat? I'm trying to do the same for a game and I'm having some problems

  • @dustydudle880
    @dustydudle880 Год назад

    Finally! An advertisement that's something I'm actually interested in, and it doesnt even ask me to go to a website for answers!

  • @CarlitoxBanana
    @CarlitoxBanana 4 месяца назад

    Best video so far!!!

  • @keviin0
    @keviin0 7 месяцев назад

    I don't know if anyone mentioned it but a good name for the _steps variable would be _subdivisions I think. As musicians, we subdivide a beat in our head to help us visualize rhythms which is essentially what you're doing with transforming something like a whole note into half notes, quarter notes, eighth notes, etc.

  • @owencoopersfx
    @owencoopersfx Год назад

    I’m working on an all-around sound and music management system for Unity native audio, and was not sure how to accurately track beats to assist with more precise music transitions. I think this is a great way to get me started. Thanks!

  • @brockproosdy8066
    @brockproosdy8066 Год назад

    absolutely brilliant video, very underrated. Saved my ass more than once

  • @overjuiced
    @overjuiced Год назад

    Fantastic tutorial. Glad I decided to consult RUclips. It was a little hard to follow at the pace of the video but after going back and forth a few times I got it. Thanks so much!

  • @wolfeygamedev1688
    @wolfeygamedev1688 Год назад +2

    super cool! I added a scriptableobject that contains the audioclip and the BPM to be able to switch songs and have the BPM for it.

    • @leomassafm160
      @leomassafm160 Год назад

      Mind sharing the code/script? I'm making a rhythm game myself and would really like something that can switch tempo on the fly without manually inserting it

    • @wolfeygamedev1688
      @wolfeygamedev1688 Год назад

      @@leomassafm160 well you still have to manually insert the bpm as a field of the SO

    • @leomassafm160
      @leomassafm160 Год назад

      @@wolfeygamedev1688 does the scriptableobject also take into account mid-song tempo changes? Rock songs have a lotta fluctuations in tempo...

  • @enchantgames_
    @enchantgames_ Год назад

    this was perfect! You are an angel!

  • @gabrielwhite9881
    @gabrielwhite9881 Год назад

    Thats so great. What a cool idea to write a script for that issue to figure out how to do things on beats. :) pls more of that kind of small helpful scripts that solving one issue.

    • @b3agz
      @b3agz  Год назад +2

      Thanks! If you have things you'd like me to tackle let me know. Can't promise I'm smart enough to solve it but I love trying to figure things like this out.

  • @_NEWN_
    @_NEWN_ 8 месяцев назад

    Very helpful. Thank you bro!

  • @JohnnieHoliday
    @JohnnieHoliday Год назад

    Nice! Thanks for support!

    • @b3agz
      @b3agz  Год назад

      Thank you for making a great tune :D

  • @random32i8
    @random32i8 Год назад

    one thing i've found doesnt work with this well is having a song with a dynamic bpm because it makes the interval variable just randomly move around

  • @sneakysunset1765
    @sneakysunset1765 3 месяца назад

    I saw this video some time ago that would actually fix some problem I had with my game. But unfortunatly I'm using FMOD and the game is porting to android. I tried a lot but honestly I think i'm going back to built in and using this I'm fed up with FMOD.

  • @cimaaaaaaa
    @cimaaaaaaa Месяц назад

    love it

  • @candenizgumus5308
    @candenizgumus5308 Год назад

    art

  • @Kiru176
    @Kiru176 Год назад

    For some reason I still have an issue with syncing. Also the bpm doesn't seem to do anything for some reason?

  • @AlexGeek
    @AlexGeek Месяц назад

    This reminds me of Pineapple on Pizza game.

  • @Demisemæn
    @Demisemæn Год назад

    bro, why you dont use the fft blackman harris formula from unity?

  • @nefelibata263
    @nefelibata263 11 месяцев назад +1

    if my BPM increases linearly, will changing the corresponding BPM variable inside my game at the same time work with this code?

    • @b3agz
      @b3agz  11 месяцев назад +1

      It should. As long as your BPM variable correct for track at any given moment, it will always be in sync with the music, even if the BPM changes. You just need to make sure your BPM variable is always right.

    • @nefelibata263
      @nefelibata263 11 месяцев назад

      @@b3agz hmm, I see. I have an incremental game with a background beat that increases linearly from 80bpm to 160bpm in 92 seconds (numbers are not exact). I can't figure out a way to make it completely sync to the whole thing, since the gameplay itself consists of a ball going back and forth, at a certain speed that increases every x seconds.
      I kind of came back to hardcoding certain speeds, since I couldn't find a way to change the speed according to the increasing BPM down to floating point precision, but your tutorial was still really informative. the problem is that it cannot be synced at really fast speeds since it desyncs so easily and I can't find the exact 15+ decimal points number for acceleration to keep it consistent with the beat.
      thanks for the reply!

  • @_NEWN_
    @_NEWN_ 8 месяцев назад

    I had a question if you don't mind. How would I make it so that if you press a certain button on one of the beats per minute, something happens?

    • @b3agz
      @b3agz  8 месяцев назад

      If I'm understanding you right, you should be able to use the UnityEvents system to trigger a "beat" and then just check if the beat is currently triggered.

  • @felixfontain6423
    @felixfontain6423 Год назад

    When comes the Showreveal of Jam3

    • @b3agz
      @b3agz  Год назад

      Should be up towards the end of this coming week.

  • @davidharmon7467
    @davidharmon7467 Год назад

    Simples!

  • @RazeGames654
    @RazeGames654 Год назад

    Can it be used in 2D objects??

    • @b3agz
      @b3agz  Год назад

      Yes, you can use it with any game object. There's no fundamental difference between a 2d and a 3d object to unity.

  • @felixfontain6423
    @felixfontain6423 Год назад

    First

  • @uuhhhhhhhhhhhhhhhhhhhhhhhhhh
    @uuhhhhhhhhhhhhhhhhhhhhhhhhhh 11 месяцев назад

    Thank you, @b3agz! 🔊🔊🔊🔊🔊

  • @Sacroooooooooooooooooooooooooo

    FOR PEOPLE WHO PUT IN THIS CODE BEFORE REALISING IT'S FOR BEATS, NOT PRE RECORDED TRACKS
    First of all, you need ```using System.Linq;```
    This might take a while, but you can add ```[SerializeField] private float[] _skips;``` to the start of Intervals and then do ```if(!(_skips.Contains(_lastInterval))) { // _trigger.invoke, etc}```. Then you can put every note to skip if there is no beat there.

    • @Kiru176
      @Kiru176 Год назад

      I have a pre-made music with its beat being a core part. I now wanted to use it for a rhythm puzzle game. Can you explain this solution? Somehow my object is still out of sync with TestBeat() and the BPM variable doesn't seem to be affecting it for some reason. I'm still looking through it.

    • @Sacroooooooooooooooooooooooooo
      @Sacroooooooooooooooooooooooooo Год назад +2

      @@Kiru176 I'll make a google doc. It might help.

    • @Sacroooooooooooooooooooooooooo
      @Sacroooooooooooooooooooooooooo Год назад

      @@Kiru176 docs.google.com/document/d/1REb2SxXYiFDRq_n2ceY-ue_wvWAV-umKf64Xzk9BgYc/edit?usp=sharing

    • @Kiru176
      @Kiru176 Год назад

      @@Sacroooooooooooooooooooooooooo Thank you.

    • @victornogueira2992
      @victornogueira2992 9 месяцев назад +1

      @@Sacroooooooooooooooooooooooooodid you make it?