I’m setting up a “waypoint” system where I click where I want an object to move but when I use:
private Camera cam;
Vector3 point = new Vector3();
Vector3 mousePos = new Vector3();
public Transform particle;
void Start()
{
cam = Camera.main;
}
void OnGUI()
{
Event currentEvent = Event.current;
// Get the mouse position from Event.
// Note that the y position from Event is inverted.
mousePos.x = currentEvent.mousePosition.x;
mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;
point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
GUILayout.BeginArea(new Rect(20, 20, 250, 120));
GUILayout.Label("Screen pixels: " + cam.pixelWidth + ":" + cam.pixelHeight);
GUILayout.Label("Mouse position: " + mousePos);
GUILayout.Label("World position: " + point.ToString("F3"));
GUILayout.EndArea();
}
private void Update()
{
if (Input.GetKeyDown("m"))
{
Instantiate(particle, point, Quaternion.identity);
}
}
from Unity Docs but the z its uses is the camera position. Is there a way to get the z in world space?