Mouse click on plane with specific Camera settings

Hello,

I have a problem that I tried to solve using various YouTube tutorials and answers from this forum and I’ve managed to narrow it down to a camera issue, but I understand now that I can’t solve it without help, so I’ve decided to ask here for assistance.

Namely, I’ve set up a Camera in an RTS fashion that suits my needs and spawned a number of spheres on the plane. Each sphere is clickable and changes color on click. I want to be able to guide an object on the same plane where spheres are by clicking on the empty space so I’ve set up a plane and detected a mouse click:

            Plane plane = new Plane(Vector3.up, Vector3.zero);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (plane.Raycast(ray, out float entry))
            {
               // here I create an object in order to be able to determine
               // the offset that is appearing, but its not offset it seems
                var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var hitPoint = ray.GetPoint(entry);
                go.transform.position = hitPoint;
            }

I’ve noticed that position is not correct and that coordinates are off.

However, I’ve spent some time trying to solve it and I understand now that problem is appearing most likely due to the camera position and rotation (see image) and that when I click under red sphere

it is not translating my exact click point correctly. When I deleted my RTS camera, and used Main camera that was reset to 0 it was a bit jerky (position kept shifting slightly) but an object I created appeared exactly where I clicked on screen. That game angle is not right for me, so I changed back to different rotation, and again object started instantiating again in a very narrow area to the left(see image)

Can someone suggest how to overcome camera rotation when capturing mouse click?

Thanks in advance,
LD

Everything you are doing above seems 100% on point and correct.

The ONLY thing I see suspicious is Line 3: Camera.main is a shortcut to the first camera it finds that is tagged “Main Camera”

You mention you have two cameras in your game, the main and the RTS camera, which you deleted. Was it perhaps using the main camera all the time? It’s not necessarily the one you’re looking through… :slight_smile:

EDIT: as Praetor noted below, yeah, your screenshot shows an untagged camera. That’s NOT the one it is using. At least eliminate that as a possible source of error.

1 Like

Camera position and angle will not cause any problem with the code you have shared. The ray created by ScreenPointToRay will take all of these things into account.
Your code and setup seem completely fine to me… Is there any other code that could be moving/repositioning the cube?

Edit: Kurt is probably right - I notice your camera from the screenshot is “Untagged”. Camera.main is probably finding a different camera then. It only finds cameras with the “MainCamera” tag.

1 Like

Thank you both very much, you are correct! When I changed my Main camera tag from “MainCamera” to “Untagged” and RTS Camera tag from “Untagged” to “MainCamera” it actually worked! I wasn’t aware that Camera.main is a shortcut.

Much obliged.

1 Like