在更改新的随机速度之前如何等待秒数?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class GateControl : MonoBehaviour

{

    public Transform door;

    public float doorSpeed = 1.0f;

    public bool randomDoorSpeed = false;

    [Range(0.3f, 10)]

    public float randomSpeedRange;


    private Vector3 originalDoorPosition;


    // Use this for initialization

    void Start()

    {

        originalDoorPosition = door.position;

    }


    // Update is called once per frame

    void Update()

    {

        if (randomDoorSpeed == true && randomSpeedRange > 0.3f)

        {

            StartCoroutine(DoorSpeedWaitForSeconds());

        }

        door.position = Vector3.Lerp(originalDoorPosition,

            new Vector3(originalDoorPosition.x, originalDoorPosition.y, 64f),

            Mathf.PingPong(Time.time * doorSpeed, 1.0f));

    }


    IEnumerator DoorSpeedWaitForSeconds()

    {

        doorSpeed = Random.Range(0.3f, randomSpeedRange);


        yield return new WaitForSeconds(3);

    }

}

在 Update 中使用 StartCoroutine 是一个坏主意。但我希望它在运行游戏时需要一个随机速度,然后等待 3 秒并更改为新的随机速度,然后再等待 3 秒并更改为另一个新的随机速度,依此类推。


在等待 3 秒以保持当前速度不变直到下一次更改时。


慕田峪7331174
浏览 170回答 3
3回答

有只小跳蛙

您已经知道协程应该从Start:void Start(){&nbsp; &nbsp; &nbsp;//initialization&nbsp; &nbsp; &nbsp;StartCoroutine(DoorSpeedWaitForSeconds());}因此,使协程成为具有适当终止条件的循环:IEnumerator DoorSpeedWaitForSeconds(){&nbsp; &nbsp; var delay = new WaitForSeconds(3);//define ONCE to avoid memory leak&nbsp; &nbsp; while(IsGameRunning)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(randomDoorSpeed == true && randomSpeedRange > 0.3f)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doorSpeed = Random.Range(0.3f, randomSpeedRange);&nbsp; &nbsp; &nbsp; &nbsp; if(!randomDoorSpeed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doorSpeed = 1;//reset back to original value&nbsp; &nbsp; &nbsp; &nbsp; yield return delay;//wait&nbsp; &nbsp; }}对于你问的另一个问题,如果你考虑一下,你不可能使用基于 Time.time 的动态速度的乒乓。你需要像这样改变它:bool isRising = true;float fraq = 0;void Update(){&nbsp; &nbsp; if (isRising)&nbsp; &nbsp; &nbsp; &nbsp; fraq += Time.deltaTime * doorSpeed;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; fraq -= Time.deltaTime * doorSpeed;&nbsp; &nbsp; if (fraq >= 1)&nbsp; &nbsp; &nbsp; &nbsp; isRising = false;&nbsp; &nbsp; if (fraq <= 0)&nbsp; &nbsp; &nbsp; &nbsp; isRising = true;&nbsp; &nbsp; fraq = Mathf.Clamp(fraq, 0, 1);&nbsp; &nbsp; door.position = Vector3.Lerp(originalDoorPosition,&nbsp; &nbsp; &nbsp; &nbsp; new Vector3(originalDoorPosition.x, originalDoorPosition.y, 64f),&nbsp; &nbsp; &nbsp; &nbsp; fraq);}

犯罪嫌疑人X

您可以在没有协程的情况下解决原始问题:public float timeBetweenChangeSpeed = 3f;public float timer = 0;void Update (){&nbsp; &nbsp; // Add the time since Update was last called to the timer.&nbsp; &nbsp; timer += Time.deltaTime;&nbsp; &nbsp; // If 3 seconds passed, time to change speed&nbsp; &nbsp; if(timer >= timeBetweenChangeSpeed)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; timer = 0f;&nbsp; &nbsp; &nbsp; &nbsp; //Here you call the function to change the random Speed (or you can place the logic directly)&nbsp; &nbsp; &nbsp; &nbsp; ChangeRandomSpeed();&nbsp; &nbsp; }}还有关于开门和关门。这是我在我参与的迷宫游戏中用来控制门的脚本:您需要设置空游戏对象以设置边界,即直到您想要将门移动到打开或关闭时的哪一点。您将这个空的游戏对象放置在您的场景中,并将它们链接到正确字段中的脚本。该脚本将自行使用 transform.position 组件。当角色接近时,还有一个触发器输入来激活门。如果你不需要那部分,我明天可以编辑代码。您还可以使用此脚本移动平台、敌人……一般而言,任何可以直线移动的东西。using UnityEngine;using System.Collections;public class OpenDoor : MonoBehaviour {&nbsp; &nbsp; // define the possible states through an enumeration&nbsp; &nbsp; public enum motionDirections {Left,Right};&nbsp; &nbsp; // store the state&nbsp; &nbsp; public motionDirections motionState = motionDirections.Left;&nbsp; &nbsp; //Variables for State Machine&nbsp; &nbsp; bool mOpening = false;&nbsp; &nbsp; bool mClosing = false;&nbsp; &nbsp; //bool mOpened = false;&nbsp; &nbsp; //OpenRanges to open/close the door&nbsp; &nbsp; public int OpenRange = 5;&nbsp; &nbsp; public GameObject StopIn;&nbsp; &nbsp;&nbsp; &nbsp; public GameObject StartIn;&nbsp; &nbsp; //Variables for Movement&nbsp; &nbsp; float SpeedDoor = 8f;&nbsp; &nbsp; float MoveTime = 0f;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; int CounterDetections = 0;&nbsp; &nbsp; void Update () {&nbsp; &nbsp; &nbsp; &nbsp; // if beyond MoveTime, and triggered, perform movement&nbsp; &nbsp; &nbsp; &nbsp; if (mOpening || mClosing) {/*Time.time >= MoveTime && */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Movement();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void Movement()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(mOpening)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = Vector3.MoveTowards(transform.position, StopIn.transform.position, SpeedDoor * Time.deltaTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Vector3.Distance(transform.position, StopIn.transform.position) <= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mOpening = false;&nbsp; &nbsp; &nbsp; &nbsp; }else{ //This means it is closing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = Vector3.MoveTowards(transform.position, StartIn.transform.position, SpeedDoor * Time.deltaTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Vector3.Distance(transform.position, StartIn.transform.position) <= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mClosing = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // To decide if door should be opened or be closed&nbsp; &nbsp; void OnTriggerEnter(Collider Other)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; print("Tag: "+Other.gameObject.tag);&nbsp; &nbsp; &nbsp; &nbsp; if(Other.gameObject.tag == "Enemy" || Other.gameObject.tag == "Player" || Other.gameObject.tag == "Elevator")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CounterDetections++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!mOpening)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Opening();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; void OnTriggerStay(Collider Other)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(Other.gameObject.tag == "Elevator")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!mOpening)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Opening();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void OnTriggerExit(Collider Other)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(Other.gameObject.tag == "Enemy" || Other.gameObject.tag == "Player")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CounterDetections--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(CounterDetections<1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Closing();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void Opening()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mOpening = true;&nbsp; &nbsp; &nbsp; &nbsp; mClosing = false;&nbsp; &nbsp; }&nbsp; &nbsp; void Closing()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mClosing = true;&nbsp; &nbsp; &nbsp; &nbsp; mOpening = false;&nbsp; &nbsp; }}

达令说

使用计时器并设置间隔。每次达到间隔时都会触发委托事件。&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;t&nbsp;=&nbsp;new&nbsp;Timer&nbsp;{Interval&nbsp;=&nbsp;3000}; &nbsp;&nbsp;&nbsp;&nbsp;t.Elapsed&nbsp;+=&nbsp;(sender,&nbsp;args)&nbsp;=>&nbsp;{&nbsp;/*&nbsp;code&nbsp;here&nbsp;*/};
打开App,查看更多内容
随时随地看视频慕课网APP