創(chuàng)建空對象RandomObject及對應(yīng)C#腳本RandomObject
在腳本中聲明數(shù)組RandomObjects
用于保存生成對象的類型,在project文件中拖入對象。文章來源:http://www.zghlxwxcb.cn/news/detail-552161.html
按下S鍵在x方向上隨機產(chǎn)生一種游戲?qū)ο?/h2>
public GameObject[] randomObjects;
private float zBound = 30f;
private float xBound = 10f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
float xrange=Random.Range(-xBound,xBound);
int index = Random.Range(0, randomObjects.Length);
Vector3 pos = new Vector3(xrange, 0,zBound);
Instantiate(randomObjects[index],pos , randomObjects[index].transform.rotation);
}
}
將腳本綁定對象
public GameObject[] randomObjects;
private float zBound = 30f;
private float xBound = 10f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
float xrange=Random.Range(-xBound,xBound);
int index = Random.Range(0, randomObjects.Length);
Vector3 pos = new Vector3(xrange, 0,zBound);
Instantiate(randomObjects[index],pos , randomObjects[index].transform.rotation);
}
}
先將腳本拖到一個對象上,然后點擊檢查器-覆蓋-應(yīng)用到全部,這樣將使所有預(yù)制件都擁有該屬性。文章來源地址http://www.zghlxwxcb.cn/news/detail-552161.html
到了這里,關(guān)于Unity--隨機生成游戲?qū)ο蟮奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!