试图制作一个生成预制对象的脚本
Trying to make a a script that spawns that spawns a prefab object
我一直在尝试制作一个脚本,该脚本会在随机时间在 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);
}
}
嗯,哇,这里有很多解释。欢迎使用 Unity...呃,欢迎编程!
您需要将 Update 大写才能识别为 Unity 更新循环。
此外,您要从 .. 而不是 1.
之间的时间减去 Time.deltaTime
此外,您要确保将 timeBetween 初始化为 non-zero,也许使用 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);
}
}
}
我一直在尝试制作一个脚本,该脚本会在随机时间在 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);
}
}
嗯,哇,这里有很多解释。欢迎使用 Unity...呃,欢迎编程!
您需要将 Update 大写才能识别为 Unity 更新循环。
此外,您要从 .. 而不是 1.
之间的时间减去 Time.deltaTime此外,您要确保将 timeBetween 初始化为 non-zero,也许使用
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);
}
}
}