Dash in the direction of a parents child object

Hi everyone, I’m putting together 2D character controller that will work with both keyboard/mouse and an Xbox controller and I am trying to implement a dash function. So far, I seem to have it working for the controller input, but struggling with keyboard/mouse.

Currently, my Player gameobject has a cross hair (labelled ‘hitDetector’ in the script) attached to it, which rotates around the players circumference (using the left joystick on controller and the mouse position on keyboard/mouse). This directs the players attack and dash direction.

This works fine for the controller inputs and the player is dashing as expected, but over the past two days I just cannot get it working for keyboard and mouse. I’m getting a variety of issues, such as the dash being restricted to particular circumference and the dash distance multiplying itself way more than it should. Can anyone please glance at my code and see where I am going wrong or how to better implement it? Below is the dash code which sits in fixed update.

        if (isDashing)
        {
            if (useController)
            {
                Vector3 dashPosition = transform.position + lastMoveDirection * dashDistance;

                RaycastHit2D raycastHit = Physics2D.Raycast(transform.position, lastMoveDirection, dashDistance, dashLayerMask);
                if (raycastHit.collider != null)
                {
                    dashPosition = raycastHit.point;
                }

                rb.MovePosition(dashPosition);
                isDashing = false;
            }
            else
            {
                Vector3 dashPosition = new Vector3(hitDetector.transform.localPosition.x, hitDetector.transform.localPosition.y);
                dashPosition.Normalize();

                RaycastHit2D raycastHit = Physics2D.Raycast(transform.position, lastMoveDirection, dashDistance, dashLayerMask);
                if (raycastHit.collider != null)
                {
                    dashPosition = raycastHit.point;
                }

                rb.velocity = dashPosition * dashDistance;
                isDashing = false;
            }
        }

Vector3 direction = destination - origin;

It helps to think about it in terms of a Vector1 (just an integer). If you’re at 1, and you want to get to 6, how much and which direction do you need to travel? 5 = 6 - 1, so you need to move +5. Vector3 is just 3 floats, so it’s the same idea.

So if you use your pointer’s position in world space as the destination, and the character’s position as the origin, you can find the direction.

Another method is transform.Forward, which gives you the direction of the blue arrow gizmo in WorldSpace.