How can I make an object clone in the time that I indicate it to appear?
Hello everyone!
I'm creating a game for a project and I'm a bit stuck on this part. I've been looking for information on how to instantiate objects (or clones of an object) but most of the code found is to clone them randomly and at a random time (or as in this case, according to the bpm of the music). Although I want to use the beat of the music to indicate when a box should appear, and indeed the bpm is for that, I would like to have more control over the number of boxes and how often they appear.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts
{
public class Spawner : MonoBehaviour
{
public GameObject[] cubes;
public Transform[] points;
public float beat;
private float timer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (timer > beat)
{
GameObject cube = Instantiate(cubes[Random.Range(0, 2)], points[Random.Range(0, 2)]);
cube.transform.localPosition = Vector3.zero;
timer -= beat;
}
timer += Time.deltaTime;
}
}
}
This code is the closest thing to what I intend to do (a VR game of the Beat Saber style but boxing). The problem is that the result is that many boxes appear to be hit in a very time interval (the person who plays it can get tired in the first minute). My intention is that the boxes appear and are hit to the sound of the music, so at least I want to control the moment they appear and thus give the person who plays it some time to rest at a time when the music is calm . In other words, it's "ordering" the box to appear at 00:45 second.
If you have any suggestions for this project, you can tell me with confidence. Also if you don't understand some of the code I show I can explain it better if necessary.