How in the world do I ACCURATELY place an object at the mouse?

My game is a 2D topdown. The only collider on the scene is the on the player. The camera we’re using is inside the player.

.

Using ScreenToWorldPoint becomes more inaccurate the farther away from the player the mouse is, and using RayCasts has given me absolutely nothing.

.

I’ve copied and pasted a million scripts and none of them work. Some place the object directly at (0,0), and some don’t even place at all. Raycasts don’t even know what to do because I obviously can’t make my entire background a collider if players are moving through it.

.

This kinda works:

          Vector3 mouseposition = Input.mousePosition;
            mouseposition.z = 10;

         Vector3 pos = (cam.ScreenToWorldPoint(mouseposition) - transform.position).normalized;

         GameObject b = Instantiate(box , cam.ScreenToWorldPoint(mouseposition), Quaternion.identity);

But it “works” because it places it where your mouse is, but that is waaaay above the player. So set the Z to 0 immediately after placing so it’s on the player’s level? Well now it’s inaccurate and doesn’t place where you clicked at all!

It was easy as setting the camera from perspective to orthographic. This was far more infuriating than it should have been.

Hello, I think these topics could help you.

http://saadkhawaja.com/move-3d-object-mouse-make-collide/

You want to set the z position relative to the camera so to place objects at the same z position as the player:

Vector3 mousePos = Input.mousePosition;
mousePos.z = -cam.transform.position.z + player.transform.position.z;
Vector3 worldPos = cam.ScreenToWorldPoint(mousePos);
Instantiate(box, worldPos, Quaternion.identity);

If the z position of the player is always 0 you don’t need to add it.