Skip to content

Instantly share code, notes, and snippets.

@kirevdokimov
Created February 4, 2018 19:00
Show Gist options
  • Save kirevdokimov/82c63fadad630c0ed7753f300ecafecf to your computer and use it in GitHub Desktop.
Save kirevdokimov/82c63fadad630c0ed7753f300ecafecf to your computer and use it in GitHub Desktop.
Get mouse position z = 0
public static Vector3 GetWorldPointFromMouse(bool plane = true, float planeLevel = 0)
{
var groundPlane = new Plane(Vector3.up, new Vector3(0, planeLevel, 0));
var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
RaycastHit rayHit;
Vector3 hit = new Vector3();
float dist;
if(plane)
if (groundPlane.Raycast(ray, out dist))
hit = ray.origin + ray.direction.normalized * dist;
if (!plane)
if (Physics.Raycast(ray, out rayHit))
hit = rayHit.point;
return hit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment