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

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

Hook Mechanic 和同步 Time.Delta Time C#

Hook Mechanic 和同步 Time.Delta Time C#

C#
呼如林 2022-01-15 19:22:38
我正在處理一個新項目,其中我的核心機制之一是一個鉤子,如果一個物體被擊中,它們要么被拉動要么被摧毀?,F在我只是想讓這個運動機制工作,這是我的代碼:public class Hook : MonoBehaviour{ //Remember Couroutine is pretty much update()public Transform Target;private float Thrust; // Int for motionpublic Rigidbody rb;public float HookTravelTime = 5f; //Define float for secondsprivate bool moving = false;  //Are we currently moving?public int time; // Use this for initializationvoid Start(){    Thrust = 75f;    rb = GetComponent<Rigidbody>();    print("working");}void OnCollisionEnter(Collision col){    if (col.gameObject.name == "enemy")    {        print("xd");        Destroy(col.gameObject);    }}void ThrowHook(){    if (Input.GetKeyDown(KeyCode.H) && !moving)    {        moving = true;        var moveIncrement = new Vector3(Thrust, 0, 0 * Time.deltaTime); // setting values? difference between var and int?        while (time <= HookTravelTime)        {            time = Time.deltaTime + 1;// I want the int time to increase by 1 every "second". Cannot impicitly convert type float to iny. An explicit conversion exists.???            // How do I put VAR MOVEINCREMENT into here? I still don't understand what a var is. Why can't I just make a void()        }        if (time >= HookTravelTime)        {            transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust); // return to empty object which is in front of player.        }    }}// Update is called once per framevoid Update()    {        ThrowHook();    }}這些問題是我在代碼中注釋的,但我將通過它們。在 Void ThrowHook() 中,我試圖將游戲中每一秒的 Int(time) 減少到 +1。我試圖將其與 Time.Delta Time 同步,但這不起作用。我不知道我在這里做什么。在 ThrowHook() 中的 while 循環上方,有人告訴我創建一個 var 來在其中存儲信息(Var MoveIncrement)。我試圖將恒定運動代碼放入其中,但我不知道如何調用它。如果可能的話,你能告訴我這行代碼是否錯誤請暫時忽略 Bool Moving,因為我一直沒有始終如一地試圖解決這個問題。希望我已經把這些問題說清楚了,如果沒有,我很抱歉,我急于去橄欖球練習。TLDR:我想要一個鉤子在“時間”還沒有超過 5 秒的時候有一個恒定的力量向前。如果有,則鉤子移回玩家面前的目標位置。
查看完整描述

1 回答

?
ITMISS

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

Time.deltaTime是完成最后一幀所需的時間(以秒為單位),因此如果您的游戲以 60fps 運行,則其值為 1/60。你可能會一直使用它,因為幀的持續時間不是恒定的,所以試著很好地理解它 https://docs.unity3d.com/ScriptReference/Time-deltaTime.html


也就是說,如果你想等待 5 秒來使用 Update() 做某事:


float hookTravelTime=5f; // you can use an int if you want full seconds only, you will be able to compare float>int/float<int/ecc

float timeHookTraveling=0f; //in your case the variable named "time"(it isn't a good variable name)


bool isHookActive=false;

    void Update()

    {

//here you should check your input foor hook activation

/*

if(keyPressed...)

{

    what the hook should do on actiovation code here

    isHookActive=true;

    }

*/


//if the hook is not resting execute the code below

      if(isHookActive)

       {

//if the hook traveled for more than hookTravelTime(5 seconds in your case)

         if(timeHookTraveling>=hookTravelTime)

           {

             //your action after the 5 seconds of hook travel

             timeHookTraveling=0f;//reset the travel time for your next hook activation

             isHookActive=false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook.

           }

//if the hook didn't travel for at least 5 seconds, increase its travel time by frame's time, thisone will be executed until you reach value 5.0f

         else//or without else, whatever

           {

             timeHookTraveling+=Time.deltaTime;//increase your travel time by last frame's time

           }

       }

    }

你有問題,因為你正在轉換為一個很小的分數(正如我之前所說的,如果你在 60fps 時接近 1/60),所以它的值將始終為 0


額外:不要那樣檢查輸入,嘗試在函數中添加一些邏輯:如果(輸入)然后拋出(所以檢查輸入然后決定調用拋出)另外我建議你不要通過它的名字訪問敵人,如果你將使用一個預制你的敵人的名字將是“敵人(克隆)”,如果會有更多的敵人,ecc?使用圖層、標簽或類似的東西。


查看完整回答
反對 回復 2022-01-15
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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