Move CharacterController a specified distance away from starting position.

Hi!

I’m trying to create a Dash-move for my character using a Character Controller.

So I want to move him quickly a specified distance (4 meters) in the forward vector when I press a button, but I also want him to take slopes into account.
By that I mean that I want him to slide up the slope until he’s 4 meters away from his starting position (you can visualise this by having a sphere with a radius of 4 meters on the character, and the extents of that sphere is how far I want the character to be able to go in any direction). I figured using a Vector’s magnitude would be a way to do it, but haven’t been able to figure out exactly how.

I’ve included some of the code below. I’ve tried a bunch of things, and the problem with the version below is that he’ll go longer/shorter than the dashDistance sometimes because the magnitude is in world space. I tried a bit with local vectors on the character, but I’m pretty new to programming so I haven’t been able to make it work either.

Do any of you guys have any suggestions to how I could implement a dash this way?

float dashDistance = 4.0f;

IEnumerator ControllerDash()
{
    //Get the starting positions, the target position and the offset
    startPosition = transform.position;
    targetPosition = startPosition + transform.TransformDirection(Vector3.forward * dashDistance);
    offset = targetPosition - transform.position;

    //Move the character while the offset magnitude is between 0.2 and 4.2.
    while (offset.magnitude > 0.2f && offset.magnitude < dashDistance + 0.2f)
    {
        print("We're moving towards the target");

        offset = targetPosition - transform.position;
        offsetMagnitude = offset.magnitude;

        characterController.Move(transform.TransformDirection(Vector3.forward) * dashSpeed * Time.deltaTime);

        yield return null;
    }

    print("We've reached the target");
}

try changing the while to this since Vector A - B isnt the distance but the direction.

    while(4 > Vector3.Distance(startPosition, transform.position))
    {
        characterController.Move(transform.TransformDirection(Vector3.forward) * dashSpeed * Time.deltaTime);
    }