scaling an object based on the distance between it and the mouse cursor

    void Update()
    {
        //if (Input.GetMouseButtonDown(0))
        {
            reach();
        }

    }

    void reach()
    {
        // find screen pos of tongue
        Vector2 tongue_pos = Camera.main.WorldToViewportPoint(transform.position);

        // find screen pos of mouse
        Vector2 mouse_pos = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);

        // calculate angle between points
        float angle = Mathf.Atan2(tongue_pos.y - mouse_pos.y, tongue_pos.x - mouse_pos.x) * Mathf.Rad2Deg;

        // calculate the distance between the points
        float distance = Vector2.Distance(tongue_pos, mouse_pos);

        // rotate tongue to face mouse
        transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
        // stretch tongue out to cursor
        transform.localScale = new Vector3(distance*10, transform.localScale.y, transform
}

I’m trying to stretch this object on it’s localscale.x out to the position of the cursor. I thought setting the localScale.x to the distance between the object’s pivot and the position of the mouse cursor would do it, but that didn’t seem to work. Multiplying that distance by 10 seems to work when the object is scaling vertically but not horizontally. I can’t work out what the problem is. Any advice?

The unit for the distance should be in World unit, so the tongue_pos and mouse_pos should be in the World space too.

So, the toungue_pos should be modified as:

// find world pos of tongue
Vector2 tongue_pos = transform.position;

And the mouse_pos should be modified as:

// find world pos of mouse
Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);

Also, don’t multiply the distance by 10.

transform.localScale = new Vector3(distance, transform.localScale.y, transform.localScale.z);