Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closevector | The vector to project on the plane. |
planeNormal | The normal which defines the plane to project on. |
Vector3
The orthogonal projection of vector
on the plane.
Projects a vector onto a plane.
For a given plane described by planeNormal
and a given vector vector
,
Vector3.ProjectOnPlane generates a new vector orthogonal to planeNormal
and parallel to the plane.
Note: planeNormal
does not need to be normalized.
The red line represents @@vector@@, the yellow line represents @@planeNormal@@, and the blue line represents the projection of @@vector@@ on the plane.
The script example below makes Update
generate a vector
position, and a planeNormal
normal. The Vector3.ProjectOnPlane static method receives the arguments and returns the Vector3 position.
using System.Collections; using System.Collections.Generic; using UnityEngine;
// Vector3.ProjectOnPlane - example
// Generate a random plane in xy. Show the position of a random // vector and a connection to the plane. The example shows nothing // in the Game view but uses Update(). The script reference example // uses Gizmos to show the positions and axes in the Scene.
public class Example : MonoBehaviour { private Vector3 vector, planeNormal; private Vector3 response; private float radians; private float degrees; private float timer = 12345.0f;
// Generate the values for all the examples. // Change the example every two seconds. void Update() { if (timer > 2.0f) { // Generate a position inside xy space. vector = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0.0f);
// Compute a normal from the plane through the origin. degrees = Random.Range(-45.0f, 45.0f); radians = degrees * Mathf.Deg2Rad; planeNormal = new Vector3(Mathf.Cos(radians), Mathf.Sin(radians), 0.0f);
// Obtain the ProjectOnPlane result. response = Vector3.ProjectOnPlane(vector, planeNormal);
// Reset the timer. timer = 0.0f; } timer += Time.deltaTime; }
// Show a Scene view example. void OnDrawGizmosSelected() { // Left/right and up/down axes. Gizmos.color = Color.white; Gizmos.DrawLine(transform.position - new Vector3(2.25f, 0, 0), transform.position + new Vector3(2.25f, 0, 0)); Gizmos.DrawLine(transform.position - new Vector3(0, 1.75f, 0), transform.position + new Vector3(0, 1.75f, 0));
// Display the plane. Gizmos.color = Color.green; Vector3 angle = new Vector3(-1.75f * Mathf.Sin(radians), 1.75f * Mathf.Cos(radians), 0.0f); Gizmos.DrawLine(transform.position - angle, transform.position + angle);
// Show the projection on the plane as a blue line. Gizmos.color = Color.blue; Gizmos.DrawLine(Vector3.zero, response); Gizmos.DrawSphere(response, 0.05f);
// Show the vector perpendicular to the plane in yellow Gizmos.color = Color.yellow; Gizmos.DrawLine(vector, response);
// Now show the input position. Gizmos.color = Color.red; Gizmos.DrawSphere(vector, 0.05f); Gizmos.DrawLine(Vector3.zero, vector); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.