1 回答

TA貢獻1810條經驗 獲得超5個贊
Vector3.MoveTowards 需要每幀運行,這就是我* Time.deltaTime在評論中提到的原因。
在 5s 時,rb 的速度變為零并isHookActive變為假,因此Vector3.MoveTowards不稱為每幀。
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
{
print("hehemeth");
rb.velocity = Vector3.zero; //negate addforce from before
isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
timeHookTraveling = 0f;//reset the travel time for your next hook activation
}
else//if you havent hit 5 keep increasing
{
timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
}
}
else if (!isHookActive && transform.position != Target.position)
{
transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust * Time.deltaTime);
}
一個更好的方法是把
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
進入FixedUpdate()但不是Update()同時
if (isHookActive )
{... }
留在Update().
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報