How to move a game-object to my mouse using Camera.ScreenToWorldPoint

I have been trying to get a gameobject to jump to where my mouse cursor is.

What had worked is to have a ground plane, and use Camera.WorldToScreenRay. However, I am curious if it is possible to have the same effects using Camera.ScreenToWorldPoint.

I also the issue is that I have no idea what to set for the z component of the input vector to the function.

1 Answer

1

You can use the camera's Forward vector. Cast a ray to some length, if it hits something, put it there, if not, put it at some maximum length away.

In my case, the floor plane will always be at y = 0. So setting the z component of the vector to the negative of the camera's y works.