-1

How can i stay in a function for a certain amount of time? I know about coroutines, so i should call coroutines every times for simple things? For example: when i press the key, at that time call function, which slowly move the character. The function fires instantly, that's why it doesn't work

void Update()
{
    RaycastHit2D hitPlayer = Physics2D.Raycast(_tr.position,new Vector2(-1,0), _distance, LayerMask.GetMask("Player"));
    if (hitPlayer.collider != null && Input.GetKeyDown(KeyCode.Space))
    {
        MovingToBox(hitPlayer);
    }
}

private void MovingToBox(RaycastHit2D hitPlayer)
{
    
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Nikolay
  • 21
  • 2

2 Answers2

0

Yeah, depending on your final goal your could use a coroutine.

You could also want to use a float timer :

  • When your press your key, set that timer to whaterver x seconds you want
  • In your Update loop, move your character by a tiny bit and remove Time.deltaTime from your timer

The first option is simpler, but the second gives you more flexibility

Isaki
  • 109
  • 5
0

If you’re repeating the interpolation multiple times, then I would do a coroutine. I find that with Slerping and Lerping, coroutines are nice.

LFulla
  • 1