【问题标题】:How To randomize timer duration between two floats如何在两个浮点数之间随机化计时器持续时间
【发布时间】:2019-11-19 12:05:41
【问题描述】:

目前,此代码生成我的预制件时只有 1 个浮点数,即 1 秒或 w.e.我想在最小浮动和最大浮动之间生成我的预制件,但不知道该怎么做,因为我是新手,还在学习 c# 和统一。

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;
        }
    }


}

【问题讨论】:

    标签: c# unity3d clone instantiation


    【解决方案1】:

    fTimeIntervals 字段拆分为fMaxTimeInterval 字段和fMinTimeInterval 字段,然后在重置计时器时使用Random.Range 将其设置为这些间隔之间的值。您甚至可以在 StartUpdate 中创建 ResetTimer 方法来执行此操作,因此如果您更改操作方式,只需在一个地方进行更改:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Spawn : MonoBehaviour
    {
    
        [SerializeField]
        public GameObject coin;
    
        [SerializeField]
        float fMinTimeInterval;
    
        [SerializeField]
        float fMaxTimeInterval;
    
        [SerializeField]
        Vector2 v2SpawnPosJitter;
    
        float fTimer = 0;
    
        public CurrencyManager cm;
    
    
        // Start is called before the first frame update
        void Start()
        {
            ResetTimer();
        }
    
        // Update is called once per frame
        void Update()
        {
    
            fTimer -= Time.deltaTime;
            if (fTimer <= 0)
            {
                ResetTimer();
                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;
            }
        }
    
        void ResetTimer()
        {
            fTimer = Random.Range(fMinTimeInterval,fMaxTimeInterval);
        }
    }
    

    【讨论】:

    • 那么,复位定时器有什么作用呢?希望它不会与我在设定时间后破坏预制件的自动销毁脚本冲突?
    • 它不会阻止游戏对象被销毁。此外,您几乎肯定不会破坏预制件,而是您从预制件实例化的游戏对象 :)
    • 好的,试过你给我的脚本似乎有效?我需要添加什么吗?谢谢
    【解决方案2】:

    不太明白你的意思,你是说计时器吗? 如果您想要一个介于最小值/最大值之间的随机数,请使用 random.range https://docs.unity3d.com/ScriptReference/Random.Range.html

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 2020-06-28
      • 2021-01-03
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多