C# 如何根据音频剪辑的长度实例化对象
我的总体目标是创建一个脚本,该脚本沿着y轴不断生成一个对象,直到一段音频停止播放。所讨论的物体是“星场”,它是一组创造外层空间效果的恒星。这是很容易做到的,我已经知道如何用恒定的速度做到这一点。对我来说,最困难的部分是用一个越来越快的对象生成这些对象 为了详细说明这一点,有一个火箭使用下面的脚本运行得越来越快,它将我设定的恒定速度(10)乘以矢量3的运动。正如您在代码中看到的,它的加速度为0.25。这个想法是火箭继续飞行,直到音频结束,星星停止繁殖,游戏结束。火箭越来越快,所以我不能硬编码 下面是我的火箭和星际繁殖脚本代码 这只是解决问题的一种方法,我花了相当长的时间尝试不同的东西,但似乎没有任何效果。我觉得有一种更容易解决这个问题的方法 星号繁殖代码:C# 如何根据音频剪辑的长度实例化对象,c#,unity3d,audio,vector,instantiation,C#,Unity3d,Audio,Vector,Instantiation,我的总体目标是创建一个脚本,该脚本沿着y轴不断生成一个对象,直到一段音频停止播放。所讨论的物体是“星场”,它是一组创造外层空间效果的恒星。这是很容易做到的,我已经知道如何用恒定的速度做到这一点。对我来说,最困难的部分是用一个越来越快的对象生成这些对象 为了详细说明这一点,有一个火箭使用下面的脚本运行得越来越快,它将我设定的恒定速度(10)乘以矢量3的运动。正如您在代码中看到的,它的加速度为0.25。这个想法是火箭继续飞行,直到音频结束,星星停止繁殖,游戏结束。火箭越来越快,所以我不能硬编码 下面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StarSpawn : MonoBehaviour {
// Use this for initialization
public Transform Object;
AudioSource Music;
float MusicClipLength;
float distance;
void Start()
{
Music = GetComponent<AudioSource>();
AudioClip MusicClip;
MusicClip = Music.clip;
MusicClipLength = Music.clip.length; //time
distance = ((((0.5f * 0.25f) * (MusicClipLength * MusicClipLength)))); //distance
float RoundedDistance = (float)Mathf.Floor(distance); //rounding
for (int i = 0; i <= RoundedDistance; i++) //generation loop
{
Instantiate(Object, new Vector3(0, i * 1750.0f, 500),
Quaternion.Euler(-90, 0, 90));
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类星群:单行为{
//用于初始化
公共转化对象;
音频源音乐;
浮动音乐长度;
浮动距离;
void Start()
{
Music=GetComponent();
音频剪辑音乐唇;
MusicClip=Music.clip;
MusicClipLength=Music.clip.length;//时间
距离=(((0.5f*0.25f)*(MusicClipLength*MusicClipLength));//距离
float RoundedDistance=(float)Mathf.Floor(距离);//舍入
对于(int i=0;i 0)
{
rb.AddForce(移动*速度);
}
}
}
}
0.25是所有物体的加速度,10是设定的公共速度
我意识到这是一个需要解决的大问题,所以即使有人认识到解决问题的更简单方法,我也非常感谢任何建议
编辑
我的实际问题是,恒星过度繁殖,太多了。因此,当音频结束时,仍然有很多明星留下。在这里使用协同程序可能是一个不错的选择。这样,你就可以利用火箭的速度来确定恒星繁殖之间的时间延迟:
float totaltime;
void Start()
{
Music = GetComponent<AudioSource>();
AudioClip MusicClip;
MusicClip = Music.clip;
MusicClipLength = Music.clip.length; //time
distance = ((((0.5f * 0.25f) * (MusicClipLength * MusicClipLength)))); //distance
float RoundedDistance = (float)Mathf.Floor(distance); //rounding
StartCoroutine(Spawner());
}
void IEnumerator Spawner()
{
totaltime += Time.deltaTime;
while (totaltime < MusicClipLength)
{
Instantiate(Object, new Vector3(0, i * 1750.0f, 500), Quaternion.Euler(-90, 0, 90));
yield return new WaitForSeconds(rocketspeedFactor); // you should mess around with this value to get the spawning frequency correct.
// would be good to retrieve the speed from the rocket and multiple by some factor
}
}
float totaltime;
void Start()
{
Music=GetComponent();
音频剪辑音乐唇;
MusicClip=Music.clip;
MusicClipLength=Music.clip.length;//时间
距离=(((0.5f*0.25f)*(MusicClipLength*MusicClipLength));//距离
float RoundedDistance=(float)Mathf.Floor(距离);//舍入
start例程(Spawner());
}
void IEnumerator产卵器()
{
totaltime+=Time.deltaTime;
while(总时间
到底是什么问题?星星不会繁殖吗?火箭没有加速?是的,对不起,忘了提这个。问题是恒星过度繁殖,太多了。
float totaltime;
void Start()
{
Music = GetComponent<AudioSource>();
AudioClip MusicClip;
MusicClip = Music.clip;
MusicClipLength = Music.clip.length; //time
distance = ((((0.5f * 0.25f) * (MusicClipLength * MusicClipLength)))); //distance
float RoundedDistance = (float)Mathf.Floor(distance); //rounding
StartCoroutine(Spawner());
}
void IEnumerator Spawner()
{
totaltime += Time.deltaTime;
while (totaltime < MusicClipLength)
{
Instantiate(Object, new Vector3(0, i * 1750.0f, 500), Quaternion.Euler(-90, 0, 90));
yield return new WaitForSeconds(rocketspeedFactor); // you should mess around with this value to get the spawning frequency correct.
// would be good to retrieve the speed from the rocket and multiple by some factor
}
}