AddForce Teleporting instead of pushing

Whats up my Unity Fam I have this player moving script that includes a dash method.

 void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();

        float distanceToClosestEnemy = Mathf.Infinity;
        Enemy closestEnemy = null;
        Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();

       weaponaiming = GetComponent<WeaponAiming>();

    }

 
    void Update()
    {
  

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (dashCounter == 0)
            {
                Vector2 direction = new Vector2(movementjoystick.joystickVec.x, movementjoystick.joystickVec.y);
                StartCoroutine(Dash(dashTime, dashSpeed, direction));
            }
        }
    }

    void FixedUpdate()
    {
        if (canMove)
        {
            if (movementjoystick.joystickVec.y != 0)
            {
                rigidbody2d.velocity = new Vector2(movementjoystick.joystickVec.x * speed, movementjoystick.joystickVec.y * speed);
            }
            else
            {
                rigidbody2d.velocity = Vector2.zero;
            }
        }
    }

    public IEnumerator Dash (float dashDuration, float dashPower, Vector2 dashDirection)
    {
        float timer = 0;

        canMove = false;
        while (dashDuration > timer)
        {h")
            timer += Time.deltaTime;
            rigidbody2d.AddForce(dashDirection * dashPower);
        }

        canMove = true;

        yield return 0;
    }

However when the dash method is called the player just teleports instead of getting pushed. help :frowning:

You need to yield return null; inside your while() loop or else it will all happen at once.

Remember, until you yield or return, NOTHING happens. Unity is locked up solid on the current frame until your scripts return… always.

You also probably do NOT want force… because that will accumulate speed… you PUSH on something with force, and it speeds up. Your main routine is alo messing with velocity so that will conflict in some unpredictable way.

Coroutines are not the best solution here. I would use a timer and just count down by Time.deltaTime and when the timer is nonzero, move like a dash, and when it becomes zero, start moving normally, otherwise you constantly have to fiddle with keeping your main code from interfering with your coroutines.