我一直在嘗試制作一個腳本,該腳本會在隨機時間將預制件放置在 y 位置的隨機位置,所以如果有人能夠幫助我解決這個問題或調整我的代碼,那將是非常好的學徒
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RainSpawn : MonoBehaviour
{
public GameObject myPrefab;
public float timeBetweenSpawn;
void update(){
timeBetweenSpawn = 1 - Time.deltaTime;
Instantiate(myPrefab, transform.position Random.insideUnitSphere * 5, Quaternion.identity);
}
}
uj5u.com熱心網友回復:
嗯,哇,這里有很多解釋。歡迎來到 Unity...呃,歡迎來到編程!
您需要將 Update 大寫才能將其識別為 Unity 更新回圈。
此外,您想從時間之間減去 Time.deltaTime ......而不是從 1。
此外,您要確保 timeBetween 初始化為非零,可能使用
Random.Range().不確定你在 y 位置的隨機位置是什么意思.. 因為你在球體中使用隨機位置,
嘗試這個:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RainSpawn : MonoBehaviour
{
public GameObject myPrefab;
public float minTime = 1f;
public float maxTime = 5f;
public float timeBetweenSpawn = 1f;
void Start(){
timeBetweenSpawn = Random.Range(minTime,maxTime);
}
void Update(){
timeBetweenSpawn -= Time.deltaTime;
if(timeBetweenSpawn<0){
Instantiate(myPrefab, transform.position
Random.insideUnitSphere * 5, Quaternion.identity);
timeBetweenSpawn = Random.Range(minTime,maxTime);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431506.html
