Hello everybody,
My question is :
I’m in 3D and I want to work on a 2D plane (don’t ask why, please ) with z=0; and I want to create a ray between an object and my mouse position on the screen.
I first used Input.mousePosition with the z coordinate to 0. But it gives me the mouse position on the screen.
So I checked and found the solution to use Camera.main.ScreenToWorldPoint(Input.mousePosition), but it doesn’t make what I want. When I use this function, it creates a ray between me (the static camera) and the object, and not between my mouse and the object. Do you know why and how I can solve this problem?
var mousePos = Input.mousePosition;
//The vector between the player and the camera point of view (cause there is no rotation on my static camera)
var offset = Camera.main.transform.position - m_player.transform.position;
//I put this vector in the pixels world
offset =Camera.main.WorldToScreenPoint(offset);
//I create the offset to place mousePos where it should be if you take the player as referential
mousePos -= offset;
//I put the mousePos.z to this to have the point in the good plane
mousePos.z = -Camera.main.transform.position.z;
//I draw a ray between the player position and the point aimed by the cursor
Debug.DrawRay(m_player.transform.position, Camera.main.ScreenToWorldPoint(mousePos));
But it didn’t work. If you have a idea where I did wrong