How to make in-game weapon aim at location of mouse?

I have a system where the weapon on the player object is meant to be aimable seperately to the actual movement of the object, though still within what’s currently invisible on screen, aiming at wherever the mouse currently is.

As it stands, though, it only points relative forward, and stops following the rotation of the player entirely when I try certain things. Does anyone have a good method of making the weapon object rotate/aim based on the position of the mouse? As it stands i’ve been trying a mix of Quaternion.RotateTowards and Camera.main.ScreenToViewportPoint(Input.mousePosition), but this isn’t working (quick thought: Do I need the UI code block for this to function?).

Full section of code below:

void Update()
    {
        //shotLoad is the script name for the weapon object, where projectiles are being launched from, just in front of the player.
        shotLoad.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(Camera.main.ScreenToViewportPoint(Input.mousePosition)), Time.deltaTime * 10);
    }

public class ExampleClass : MonoBehaviour
{
// Angular speed in radians per sec.
float speed;

    void Update()
    {
        Vector3 targetDir = Camera.main.ScreenToViewportPoint(Input.mousePosition) - transform.position;

        // The step size is equal to speed times frame time.
        float step = speed * Time.deltaTime;

        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
        Debug.DrawRay(transform.position, newDir, Color.red);

        // Move our position a step closer to the target.
        transform.rotation = Quaternion.LookRotation(newDir);
    }
}

Copied directly from here, slightly edited to what I think you are trying to do. Don’t have a compiler to test, so let me know if that works.