Vector3.MoveTowards необходим для запуска на каждом кадре, поэтому я упомянул * Time.deltaTime
в комментарии.
Через 5 с скорость 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()
.