using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateWolf : MonoBehaviour
using System.Collections.Generic;
using UnityEngine;
public class CreateWolf : MonoBehaviour
{
//初始生成时间5秒钟
float times = 5f;
//物体
public GameObject wolf1;
GameObject targert = null;
void Start () {
}
void Update()
{
times -= Time.deltaTime; //减时间
if (times < 0) //倒计时
{
//产生物体
GameObject obj = (GameObject)Instantiate(wolf1);
int ni = Random.Range(0, 200);
int nt = Random.Range(0, 200);
//随机位置
obj.transform.position = new Vector3(ni, 0, nt);
//重新设置时间为0-10之间的一个随机数 随机时间
times = Random.Range(0, 10);
}
}
本文介绍如何在Unity3D中利用Update()函数和Random类,实现物体在场景中的随机位置生成,并设置一个随机时间间隔来决定下一次生成的时间。通过实例化物体并设置其Transform.position为随机坐标,同时更新生成时间,达到动态生成物体的效果。
6910

被折叠的 条评论
为什么被折叠?



