- 已加入
- 2019年11月16日
- 留言内容
- 12
- 编程经验
- 1-3
我有一个空的对象,它是场景中的生成器,并且我希望它每隔x倍的时间生成一个随机的预制件。.我使用此代码,但无法弄清楚..我肯定有一种更简单的创建方法来自数组的随机调用。
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class obstacleSpawner : MonoBehaviour
{
//vars
public Rigidbody rb;
public float ForwardForce;
public GameObject Obstacle2;
public GameObject Obstacle1;
private Vector3 test;
//lists
public List<string> obstacles = new List<string>();
void Start()
{
obstacles.Add("Obstacle1");
obstacles.Add("Obstacle2");
InvokeRepeating("spawnObstacle", 3f, 3f);
}
void Update()
{
rb.velocity = new Vector3(rb.velocity.y, rb.velocity.y, ForwardForce);
}
void spawnObstacle()
{
System.Random r = new System.Random();
int genRand = r.Next(1, 2);
test = new Vector3(10.47f, 0.4096543f, transform.position.z);
Instantiate(obstacles[r], test, transform.rotation);
}