Combine animation and mouse position

I’m trying to animate my character but i want to go through the animation according the mouse position.
In the game, the player will try to catch some fish spawned on a random position.
The player has an animation who show im bending on a side but i want to bend the character following the mouse position.
For the moment i’ve written a script who launch the animation depending on the position of the mouse but it don’t work as i want :

 public float screenPart;
    Animator anim;

    private void Start() {
        screenPart = Screen.width / 3;
        anim = GetComponent<Animator>();

    void Update()
        //If the game is not paused
        if (Time.timeScale == 1)
            //If the mouse is in the bottom screen
            if (Input.mousePosition.y <= Screen.height/2)
                //If mouse is clicked
                 if (Input.GetMouseButton(0))
                    if (Input.mousePosition.x <= screenPart)
                        anim.SetInteger("State", 0);
                    if (Input.mousePosition.x >= screenPart && Input.mousePosition.x <= screenPart * 2)
                        anim.SetInteger("State", 1);

                    if (Input.mousePosition.x >= 2 * screenPart)
                        anim.SetInteger("State", 2);


I was thinking about set the animation to 1s and then jump to a keyframe with the mouse position, but i don’t know if it’s the right approach of the problem.
I’ve started to use a blend tree but how modify the value with the mouse position ? And this method will follow the mouse pointer ?

I don’t know if my description is enough, tell me if want more details.

This should work. First make sure you got your blend tree setup like the image, them add the script to your player, then add your animator to the public variable and hit play to test it.


using UnityEngine;

public class AnimBlendWithMousePosition : MonoBehaviour
    // Change this value to make it blend slower or faster
    public float blendMultiplier = 2f;
    public Animator _Animator;
    [SerializeField] private float mousePosX;

    private void Update()
        mousePosX = GetMousePosX_As_NegativeOneToPositiveOne();
        // Your animator should have a parameter named Blend or change this string to match 
        // the name or your paramenter
        if (_Animator != null)
            _Animator.SetFloat("Blend", mousePosX * blendMultiplier);
            Debug.LogError( + " | Animator is missing!");

    private float GetMousePosX_As_NegativeOneToPositiveOne()
        // Store mouse pixel coordinates
        float a = Input.mousePosition.x;

        // Convert coordinates to a value that goes from (-Screen.width/2, +Screen.width/2)
        float b = a - Screen.width / 2;

        // Convert b into a value that goes from (-1, 1)
        float c = b / Screen.width * 2;

        return c;