[在此处输入图像描述][1]尝试使统一生成随机管道,这些管道在一组参数内的独特位置上穿过屏幕,我尝试过使用它,但无法使其工作,我想知道是否有任何一个你会很乐意帮助找到解决办法。
尝试使用 Next() 方法创建一个随机整数,但我无法让它工作,我对 C# 和 Unity 非常熟悉,所以任何帮助将不胜感激。
> using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using static UnityEngine.RuleTile.TilingRuleOutput;
using Random = Unity.Mathematics.Random;
public class PipeSpawn : MonoBehaviour
{
public GameObject pipe;
public float spawnRate = 2;
private float timer = 0;
public float heightoffset = 10;
// Start is called before the first frame update
void Start()
{
SpawnPipe();
}
// Update is called once per frame
void Update()
{
if (timer < spawnRate)
{
timer = timer + Time.deltaTime;
}
else
{
SpawnPipe();
timer = 0;
}
}
void SpawnPipe()
{
float lowestPoint = transform.position.y - heightoffset;
float heighistpoint = transform.position.y + heightoffset;
Instantiate(pipe, new Vector3(transform.position.x, new Random(lowestPoint, heighistpoint), 0), transform.rotation);
}
}
要在 Unity 中生成随机整数,请使用
UnityEngine.Random.Range(min, max + 1)
。请注意,第二个参数(最大值)是exclusive,因此它应该比您想要的最大值多 1。这是为了更轻松地使用数组。
例如,如果您想选择 1 到 3(含)之间的随机数,则可以使用
Random.Range(1, 4)
。