Created
February 4, 2018 19:00
-
-
Save kirevdokimov/82c63fadad630c0ed7753f300ecafecf to your computer and use it in GitHub Desktop.
Get mouse position z = 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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