Draw on a Plane surface with varying camera positions
I have the situation as shown in the picture.
I have placed the Plane
in the global position (1000, 0.0).
I have an Orthographic camera
set up on the Plane
I have the Zoom set for this by changing the Size
for the camera.
I would like to draw 2D objects on a Plane
surface. How to set correctly so that drawing catches the surface?
I try to catch a Plane first, but I think I'm doing something wrong
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.collider.gameObject.name);
}
}
.
Answer by xxmariofer · Nov 26, 2020 at 11:03 AM
thats exactly how it is done, but if for some reasons is not working try using the ScreenToWorldPoint class https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
The square is placed globally (-1000, 0, 0) (Point B).
Plane plane = new Plane(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1));
float distance;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
worldPosition = ray.GetPoint(distance);
}
Debug.Log("worldPosition : " + worldPosition);
And from this code it shows that (-1000, 0, 0) is somewhere at point A. (`mousePosition`)
What could be the reason?
wait whats exactly the plane for? maybe i am missing something?
The plan is created virtually to calculate the distance to the camera. This allows me to position the cursor on an object that is in the same location as the Plane. I found it all depends on the Camera. I use Orthographic with the Zoom option (by changing the Size). I just don't know how to include it in this. When Size = 1, all is well. But when Size is changed it no longer works
Your answer
Follow this Question
Related Questions
Change camera origin point to top left? 2 Answers
Y position of objects in Unity 1 Answer
Drawing simple shapes on Canvas,How to draw simple shapes on Canvas 2 Answers
I want to make my camera follow the player ONLY when the player is on solid ground [2D] 0 Answers
Set camera position to end animation camera position? 0 Answers