1 回答

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?使用圖層、標簽或類似的東西。
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報