How can ı make rhythm Game ?

How can ı match the beat and balls hit Like this:

The way to make the timing of your game match the beat of the music is to use the time of your audiosource for your time-based code instead of using values from the usual Time class.

More specifically…

Anytime you would normally use Time.time, you should instead use musicAudioSource.time.

Anytime you would normally use Time.deltaTime, you should use a deltaTime variable you calculate yourself each frame from the time value on the musicAudioSource.

float timeLastFrame = 0;
float deltaTime = 0;

void Update()
{
    deltaTime = musicAudioSource.time - timeLastFrame;
    timeLastFrame = musicAudioSource.time;
}

OK. but ı don’t understand. How can ı spawn the object on true coordinate ?

Try making a very small project first, where you learn how to do small tasks first. If you’re not sure how to create and position objects yet in Unity, you’re not going to be able to create a whole game … sorry. Put that on hold, and learn how to get anything working as you learn how to get closer and closer to the game you want to make. You’re not going to find spawning an object at a coordinate and playing an audio track is all that’s involved in creating a rhythm game. Creating games is an actual career … its not going to be a quick answer on a forum post.

I’ve built simple rhythm games before … and I wouldn’t know where to start answering this.

1 Like

Bro ı know using unity and coding . I developed a lot of game on unity. Problem is ı want to match with bpm and object coordinate. You gave me a time value but ı dont understand how can use it for a generate object pos

You’ll have much better luck on these forums if you ask specific questions, and if you demonstrate some effort on your part. It looks like you last four threads are simply a video of another game, asking “How do I do this?”, without any specifics. Most people will just ignore such a general question.

So, in this case, can you clearly state the specific question? It sounds like you simply need to spawn objects at a particular distance from origin based on the BPM of the song. A simple approach would be for each beat to represent a particular second (or fraction thereof) and for the objects coordinate to simply match that value. For example, if the song has beats at 0.1, 0.2, 0.3, etc, then the x-coordinate of the spawned objects could be 0.1, 0.2, 0.3, etc. If the BPM changes, the second at which the beat occurs would presumably also change.

Anyway, try something, show code when you get stuck.

1 Like

Basically above. [Edit – I wrote something different, but then saw you want the BPM … so I have nothing to add. If you want to match a specific time to the distance, then you need to decide on the units of distance to time … ]

It was very hard to know what you were really asking, what level of knowledge you have, and more importantly … what you were actually stuck on based on “How do I spawn the object on true coordinate”.

Good luck!

You are moving down the track at a certain speed. If you know your speed and how long of a time it should take to reach the object, you have all the numbers you need to know to place the object.

The key is placing the object so the timing lines up is you take the start position, then add the direction you want to move times the speed you will be moving times the time you want it to take to get there. Something like this: startPosition + directionToMove * speed * timeToTake; (startPosition and directionToMove are Vector3, speed and timeToTake are floats).

Here’s an example when you add it into the previous code snippet I gave you.

float timeLastFrame = 0;
float deltaTime = 0;

float speed = 1;

void Update()
{
    // Detect how much time has passed in the song
    deltaTime = musicAudioSource.time - timeLastFrame;
    timeLastFrame = musicAudioSource.time;

    // Move forward at your speed based on how much time has passed in the music
    transform.position += Vector3.forward * speed * deltaTime;
}

void MakeObject(float timeFromPlayer)
{
    // Make a new object
    GameObject newObj = Instantiate(objectToInstantiate) as GameObject;

    // Place the new object a specific distance from the player so it takes "timeFromPlayer" seconds to reach the object
    newObj.transform.position = transform.position + Vector3.forward * speed * timeFromPlayer;
}
3 Likes