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 /
  • Help Room /
avatar image
0
Question by paulyfam · Nov 18, 2021 at 11:03 AM · movementinterpolationrhythm

Rhythm game and interpolation question

I've been following these two tutorials (this one and this one) as well as looking at the author of the second one's open source game (as they didn't write their tutorial very well) and I've ended up with these three scripts:

number one, the conductor, which keeps track of the bpm and things, as well as just generally running everything:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Conductor : MonoBehaviour
 {
     public float songBpm;
     public float secPerBeat;
     public float songPos;
     public float songPosBeats;
     public float dspSongTime;
     public AudioSource musicSource;
 
     public float firstBeatOffset;
 
     public float beatsPerLoop;
     public int completedLoops = 0;
     public float loopPosBeats;
     public float LoopPosAnalog;
 
     public static Conductor instance;
 
     public float eigthNotes;
     public float songPosEigths;
 
     public float sixteenNote;
     public float songPosSixteen;
 
     public float[] notes;
     public int nextIndex;
     public float advanceBeats = 3;
     public GameObject NoteGO;
 
     private void Awake()
     {
         instance = this;
     }
     void Start()
     {
         musicSource = GetComponent<AudioSource>();
         secPerBeat = 60f / songBpm;
         dspSongTime = (float)AudioSettings.dspTime;
         musicSource.Play();
 
         notes = new float[] { 1, 2, 3, 4, 5 };
 
         eigthNotes = secPerBeat / 2;
         sixteenNote = secPerBeat / 4;
     }
      
     // Update is called once per frame
     void Update()
     {
         songPos = (float)(AudioSettings.dspTime - dspSongTime - firstBeatOffset);
         songPosBeats = songPos / secPerBeat;
         songPosEigths = songPos / eigthNotes;
         songPosSixteen = songPos / sixteenNote;
        //Debug.Log(notes.Length);
         if (nextIndex < notes.Length && notes[nextIndex] < songPosBeats + advanceBeats)
         {
             BeatMove musicNote = NotePool.instance.GetNote(-10, 10, 4.6f, 0, notes[nextIndex]);
             nextIndex++;
         }
         
         if(songPosBeats >= (completedLoops +1) * beatsPerLoop)
         {
             completedLoops++;
         }
         loopPosBeats = songPosBeats - completedLoops * beatsPerLoop;
         LoopPosAnalog = loopPosBeats / beatsPerLoop;
     }
 }

number two, the Note Pool script, in charge of spawning all of the notes and turning them on as well as giving each of them an individual notes[nextIndex] value:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NotePool : MonoBehaviour
 {
     public static NotePool instance;
     public GameObject notePrefab;
     public int initialAmount;
     private List<BeatMove> objList;
 
     private void Awake()
     {
         instance = this;
     }
     // Start is called before the first frame update
     void Start()
     {
         objList = new List<BeatMove>();
 
         for (int i = 0; i < initialAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(notePrefab);
             obj.SetActive(false);
             objList.Add(obj.GetComponent<BeatMove>());
         }
     }
 
     public BeatMove GetNote( float startX, float endX, float posY, float posZ, float beat)
     {
         foreach (BeatMove note in objList)
         {
             if (!note.gameObject.activeInHierarchy)
             {
                 note.Initialize(startX, endX, posY, posZ, beat);
                 note.gameObject.SetActive(true);
                 return note;
             }
         }
 
         BeatMove musicNote = ((GameObject)Instantiate(notePrefab)).GetComponent<BeatMove>();
         musicNote.Initialize(startX, endX, posY, posZ, beat);
         objList.Add(musicNote);
         return musicNote;
     }
 }

and number three, the note code itself, which is in charge of interpolation and such:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BeatMove : MonoBehaviour
 {
     float t;
     public Vector3 startPosition;
     public Vector3 target;
     float timeToReachTarget;
     public static float thisNoteBeat;
     float timeElapsed;
     public static BeatMove instance;
 
     private void Start()
     {
         this.gameObject.active = true;
         instance = this;
         startPosition = transform.position;
         target = new Vector3(0, transform.position.y);
     }
 
     public void Initialize(float startX, float endX, float posY, float posZ, float beat) {
          void Update()
         {
             Debug.Log(thisNoteBeat);
 
             transform.position = new Vector3(startPosition.x + ((target.x - startPosition.x) * 1f) - beat - thisNoteBeat - Conductor.instance.songPosBeats / Conductor.instance.advanceBeats, transform.position.y, transform.position.z);
         }
     }
     }

and what this code is supposed to do is create a note on beats 1, 2, 3, 4, and 5, and smoothly move them to (0, 0) to the beat, so that it remains a rhythm game, but so far all it does is spawn immobile beats in the corner of the screen...

any help you can give would be appreciated, and if you see any spaghetti code, don't hesitate to help!

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

0 Replies

· Add your reply
  • Sort: 

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

227 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 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 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

How move object on Bezier path? 0 Answers

First person controls for GoogleCardboard 2 Answers

Cant Move towards Right and Jump at same time? 2 Answers

Gyro camera control with forward and backward movement 1 Answer

how to move a gameobject based on a dice roll 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