亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在更改新的隨機速度之前如何等待秒數?

在更改新的隨機速度之前如何等待秒數?

C#
慕田峪7331174 2021-11-07 19:16:33
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 秒以保持當前速度不變直到下一次更改時。
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

您已經知道協程應該從Start:


void Start()

{

     //initialization

     StartCoroutine(DoorSpeedWaitForSeconds());

}

因此,使協程成為具有適當終止條件的循環:


IEnumerator DoorSpeedWaitForSeconds()

{

    var delay = new WaitForSeconds(3);//define ONCE to avoid memory leak

    while(IsGameRunning)

    {

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

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


        if(!randomDoorSpeed)

            doorSpeed = 1;//reset back to original value


        yield return delay;//wait

    }

}

對于你問的另一個問題,如果你考慮一下,你不可能使用基于 Time.time 的動態速度的乒乓。你需要像這樣改變它:


bool isRising = true;

float fraq = 0;

void Update()

{

    if (isRising)

        fraq += Time.deltaTime * doorSpeed;

    else

        fraq -= Time.deltaTime * doorSpeed;


    if (fraq >= 1)

        isRising = false;

    if (fraq <= 0)

        isRising = true;


    fraq = Mathf.Clamp(fraq, 0, 1);



    door.position = Vector3.Lerp(originalDoorPosition,

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

        fraq);

}


查看完整回答
反對 回復 2021-11-07
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

您可以在沒有協程的情況下解決原始問題:


public float timeBetweenChangeSpeed = 3f;

public float timer = 0;


void Update ()

{

    // Add the time since Update was last called to the timer.

    timer += Time.deltaTime;


    // If 3 seconds passed, time to change speed

    if(timer >= timeBetweenChangeSpeed)

    {

        timer = 0f;

        //Here you call the function to change the random Speed (or you can place the logic directly)

        ChangeRandomSpeed();

    }


}

還有關于開門和關門。這是我在我參與的迷宮游戲中用來控制門的腳本:


您需要設置空游戲對象以設置邊界,即直到您想要將門移動到打開或關閉時的哪一點。您將這個空的游戲對象放置在您的場景中,并將它們鏈接到正確字段中的腳本。該腳本將自行使用 transform.position 組件。當角色接近時,還有一個觸發器輸入來激活門。如果你不需要那部分,我明天可以編輯代碼。


您還可以使用此腳本移動平臺、敵人……一般而言,任何可以直線移動的東西。


using UnityEngine;

using System.Collections;


public class OpenDoor : MonoBehaviour {


    // define the possible states through an enumeration

    public enum motionDirections {Left,Right};

    // store the state

    public motionDirections motionState = motionDirections.Left;


    //Variables for State Machine

    bool mOpening = false;

    bool mClosing = false;


    //bool mOpened = false;


    //OpenRanges to open/close the door

    public int OpenRange = 5;

    public GameObject StopIn;   

    public GameObject StartIn;


    //Variables for Movement

    float SpeedDoor = 8f;

    float MoveTime = 0f;    


    int CounterDetections = 0;



    void Update () {

        // if beyond MoveTime, and triggered, perform movement

        if (mOpening || mClosing) {/*Time.time >= MoveTime && */

            Movement();

        }

    }


    void Movement()

    {

        if(mOpening)

        {

            transform.position = Vector3.MoveTowards(transform.position, StopIn.transform.position, SpeedDoor * Time.deltaTime);


            if(Vector3.Distance(transform.position, StopIn.transform.position) <= 0)

                mOpening = false;


        }else{ //This means it is closing


            transform.position = Vector3.MoveTowards(transform.position, StartIn.transform.position, SpeedDoor * Time.deltaTime);


            if(Vector3.Distance(transform.position, StartIn.transform.position) <= 0)

                mClosing = false;


        }

    }


    // To decide if door should be opened or be closed

    void OnTriggerEnter(Collider Other)

    {

        print("Tag: "+Other.gameObject.tag);


        if(Other.gameObject.tag == "Enemy" || Other.gameObject.tag == "Player" || Other.gameObject.tag == "Elevator")

        {

            CounterDetections++;

            if(!mOpening)

                Opening();

        }       

    }


    void OnTriggerStay(Collider Other)

    {

        if(Other.gameObject.tag == "Elevator")

        {

            if(!mOpening)

                Opening();

        }

    }


    void OnTriggerExit(Collider Other)

    {

        if(Other.gameObject.tag == "Enemy" || Other.gameObject.tag == "Player")

        {

            CounterDetections--;

            if(CounterDetections<1)

                Closing();

        }

    }


    void Opening()

    {

        mOpening = true;

        mClosing = false;

    }


    void Closing()

    {

        mClosing = true;

        mOpening = false;

    }

}


查看完整回答
反對 回復 2021-11-07
?
達令說

TA貢獻1821條經驗 獲得超6個贊

使用計時器并設置間隔。每次達到間隔時都會觸發委托事件。

    var t = new Timer {Interval = 3000};
    t.Elapsed += (sender, args) => { /* code here */};


查看完整回答
反對 回復 2021-11-07
  • 3 回答
  • 0 關注
  • 194 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號