'How to move a 2D object to the mouse location at a certain speed in Unity

I've recently been attempting to get a 'roll' system implemented in my 2D Unity RPG wherein the player character rolls towards the mouse location at a certain speed. However, the script I am using currently makes the player character move up and to the right, no matter the position of the curser. Occasionally, if I put the curser in the very bottom left corner the player character my move up and to the centre or left, but this is still not what I want. Any help would be appreciated, thanks.

    void handleDodge() //BEFORE DODGE
    {
        Vector3 mousePos = Input.mousePosition;
        worldPosition = camera.WorldToScreenPoint(mousePos);
        if (Input.GetKeyDown("space"))
        {
            state = State.Roll;
            dodgeDir = (mousePos - transform.position).normalized;
            dodgeSpeed = 100f;
            
        }
    }

    void handleDodgeRolling() //DURING DODGE
    {
        transform.position += dodgeDir * dodgeSpeed * Time.deltaTime;
        dodgeSpeed -= dodgeSpeed * 10f * Time.deltaTime;
        if (dodgeSpeed < 5f)
        {
            state = State.Normal;
        }
    }


Solution 1:[1]

Use ScreenToWorldPoint instead of WorldToScreenPoint, and use your variable worldPosition instead of mousePos when assigning your dodgeDir variable.

This should work:

    void handleDodge() //BEFORE DODGE
    {
        Vector3 mousePos = Input.mousePosition;
        worldPosition = camera.ScreenToWorldPoint(mousePos);
        if (Input.GetKeyDown("space"))
        {
            state = State.Roll;
            dodgeDir = (worldPosition - transform.position).normalized;
            dodgeSpeed = 100f;
            
        }
    }
    
    void handleDodgeRolling() //DURING DODGE
    {
        transform.position += dodgeDir * dodgeSpeed * Time.deltaTime;
        dodgeSpeed -= dodgeSpeed * 10f * Time.deltaTime;
        if (dodgeSpeed < 5f)
        {
            state = State.Normal;
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala