ScreenToWorldPoint not working with cinemachine virtual camera
Hello,
I am trying to get the conversion of the mouse position to a coordinate in the Unity space with the function: Camera.main.ScreenToWorldPoint but it is not working properly.
In the scene I added a 2D Cinemachine camera and apparently this is what causes the issue, because I have deactivated that camera and the main camera and I have tried adding a new camera to the scene and it works correctly.
This is the code I am using:
Vector2 mousePoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
Do you have any idea how to make it work using Cinemachine's 2D camera?
Can you verify that a normal camera doesn't have that problem? Cinemachine shouldn't change the camera implementation.
Answer by mccann · Nov 17, 2019 at 05:25 PM
@pBlackmouthA
I had the same issue as you. I was trying to instantiate objects in the screen based on the screen size. It was ignoring the screen size of the virtual cameras. I didn't see anything about using Camera.main in the Cinemachine docs. I eventually just changed my Main Camera settings (z axis, Projection, size, etc). Tested with Cincemachine (brain) disabled. Worked. Turned on cinemachine again and it all just worked.
Note: I'm still using the Virtual Camera and not the 2D version of it.