Skip to content

Instantly share code, notes, and snippets.

@omikun
Created July 10, 2017 20:41
Show Gist options
  • Save omikun/78322d09c45124756786f8ee86b4a3b7 to your computer and use it in GitHub Desktop.
Save omikun/78322d09c45124756786f8ee86b4a3b7 to your computer and use it in GitHub Desktop.
Project a 3d vector onto a plane, convert 3D to 2D vector, useful for rendering a 2D indicator over 3D element or get direction of 3D element if out of frame
//taken from http://answers.unity3d.com/questions/1097270/project-a-vector3-onto-a-plane-orthographically.html
//You simply project your vector onto the normal and subtract the result from your original vector. That will give you the projected vector on the plane. If you want to project a point to a plane you first need to calculate the relative vector from a point on that plane.
Vector3 planeOrigin;
Vector3 planeNormal;
Vector3 point;
Vector3 v = point - planeOrigin;
Vector3 d = Vector3.Project(v, planeNormal.normalized);
Vector3 projectedPoint = point - d;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment