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 关注
- 121 浏览
添加回答
举报