Bullet to Mouse Position?


I’ve got some code that only fires in a straight line and I can’t figure out for the life of me how to get it to the mouse position. Can anyone help ?

public Rigidbody projectile;
public float speed = 20;

void Update () 

    if (Input.GetButtonDown("Fire1"))
             var pos = Camera.main.WorldToScreenPoint(transform.position);
             var dir = (Input.mousePosition - pos).normalized;

             Rigidbody instantiatedProjectile = Instantiate(projectile,
                                                as Rigidbody;

             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed)); 

Ray rayFromCam = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayFromCam, out hitFromCam))
hitFromCam.point //this method return Vector3

Hope this is what you need