Search Unity

Resolved Issues with rhythm game code - notes scrolling

Discussion in 'Scripting' started by zo8j2020, Jun 19, 2020.

  1. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Apologies in advance if anything is worded poorly, I'm still new to Unity!

    I was in the middle of following a tutorial online and was creating a prototype for a 2D rhythm game, DDR-style where notes scroll down from the top of the screen.

    I had created a script called GameManager that would play both the music and start the notes scrolling down when a key is pressed.

    However, because of that I had to omit a part of the script from the scrolling note script I had in place for the notes/arrows in order for them to scroll when prompted (which was fine at the time, the notes wouldn't scroll until a key was pressed) and I did, and added the other necessary code to the GameManager to replace it.

    However, now when I press "play" the notes automatically scroll down and the music only plays once I press a key!

    Below is the scrolling note code;

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class BeatScroller : MonoBehaviour
    7. {
    8.     public float beatTempo;
    9.  
    10.     public bool hasStarted;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         beatTempo = beatTempo / 60f;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.        /* if (!hasStarted)
    23.         {
    24.             if (Input.anyKeyDown)
    25.             {
    26.                 hasStarted = true;
    27.             }  [This part of the code is omitted due to the GameManager script replacing this particular part of code]*/
    28.        
    29.        
    30.        
    31.             transform.position -= new Vector3(0f, beatTempo * Time.deltaTime, 0f);
    32.         }
    33.  
    34.  
    35.     }
    And this is the GameManager code;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public AudioSource theMusic;
    8.  
    9.     public bool startPlaying;
    10.  
    11.     public BeatScroller theBS;
    12.  
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(!startPlaying)
    25.         {
    26.             if(Input.anyKeyDown)
    27.             {
    28.                 startPlaying = true;
    29.                 theBS.hasStarted = true;
    30.  
    31.                 theMusic.Play();
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    Is there anywhere I have gone wrong for this to happen? Any ideas on what I could try to do in order to fix this? Any help is greatly appreciated!
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    you are moving your beats in the update method. There was a bool preventing it from moving but you have commented that out. So access the bool in your gamemanger and only scroll if it is true.
     
  3. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thank you for your response! Unfortunately, I'm still new to Unity and I would be grateful if you were to elaborate on how I could do this?
    Edit: No need, I was able to work it out. Thank you so much for your help!
     
    Last edited: Jun 19, 2020