当前,此代码仅以1浮点数(即1秒或以下)生成我的预制件。我想在最小浮动数和最大浮动数之间生成我的预制件,但不确定该怎么做,因为我是新手,还在学习c#和unity。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Spawn : MonoBehaviour
{
[SerializeField]
public GameObject coin;
[SerializeField]
float fTimeIntervals;
[SerializeField]
Vector2 v2SpawnPosJitter;
float fTimer = 0;
public CurrencyManager cm;
// Start is called before the first frame update
void Start()
{
fTimer = fTimeIntervals;
}
// Update is called once per frame
void Update()
{
fTimer -= Time.deltaTime;
if (fTimer <= 0)
{
fTimer = fTimeIntervals;
Vector2 v2SpawnPos = transform.position;
v2SpawnPos += Vector2.right * v2SpawnPosJitter.x * (Random.value - 0.5f);
v2SpawnPos += Vector2.up * v2SpawnPosJitter.y * (Random.value - 0.5f);
GameObject cmscript = Instantiate(coin, v2SpawnPos, Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
cmscript.GetComponent<AutoDestroy>().CM = cm;
}
}
}
答案 0 :(得分:0)
不是很了解您的意思,您是说计时器吗? 如果要在最小值/最大值之间使用随机数,请使用random.range https://docs.unity3d.com/ScriptReference/Random.Range.html
答案 1 :(得分:0)
将您的22
字段拆分为fTimeIntervals
字段和fMaxTimeInterval
字段,然后在重置计时器时使用Random.Range
,以将其设置为这些间隔之间的值。您甚至可以在fMinTimeInterval
和ResetTimer
中使用Start
方法来执行此操作,因此,如果您更改操作方式,则只需在一个地方进行更改即可。
Update