How do I get mouse position in game units?

So I’m creating a game for smart phones and eventually is going to be so that there is one cannon on the top part of the screen and when I put my finger anywhere on the screen it will shoot a ball towards my finger. But I want to test it on the computer so I tried to do it like this:

if (Input.GetMouseButtonDown(0)) {
Instantiate (prefab, new Vector2(Input.mousePosition.x, (Input.mousePosition.y, Quaternion.identity);
}

But the problem is that Input.mousePosition is in pixels. Even better would be if there was an object always at the same place as the mouse in game.
So hopefully that is clear enough. And sorry for bad English it’s not my native.
-Michael

you mean u want to get the world coordinates?

if yes then:

GameObject x=Instantiate (prefab, new Vector2(Input.mousePosition.x, Input.mousePosition.y, Quaternion.identity);

public Transform target;
void Update() {
Vector3 screenPos = camera.WorldToScreenPoint(x.transform.position);

now ur gameobject has the coordinates in screen position

Hi you
can use Camera class Unity - Scripting API: Camera

You can look Event.current.mousePosition

This is your answer: