How do you make a projectile fire to your mouse click?c#

Hey there,

I was messing about today and I created a system that fired in a straight direction when i clicked, I was wondering how do you make it so can object will move to the coordinates of your mouse X,Y,Z ?

Your mouse is only a 2d point, which you are trying to put in a 3d ray. By definition it will always move straight out from the point you click on because there is no rotation or third axis. You can find the depth of the mouse in a raycast, however this requires you have an object that the ray your mouse puts out will hit.

Look at the last example:

public class example : MonoBehaviour {
    public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    public RaycastHit hit;
    void Example() {
        if (Physics.Raycast(ray, out hit, 100)) {
            Debug.DrawLine(ray.origin, hit.point);
        }
    }
}

The hit.point is where your shot should hit if you fire at the mouse position. Figure out a way to aim at hit.point and you will be firing bullets to your mouse.