Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


ProjectOnPlane

Projects a vector onto a plane.
Read time 2 minutesLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

ProjectOnPlane(Vector3, Vector3)

Projects a vector onto a plane.
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)

Parameters

vector

The vector to project on the plane.

planeNormal

The normal which defines the plane to project on.

Returns

Type

Description

Vector3The vector that results from projection of
vector
on the plane.

Remarks

This method resolves the input vector onto the plane and returns the resulting vector. The new vector is orthogonal to
planeNormal
and parallel to the plane.
Note that
planeNormal
does not need to be normalized.
// This example rotates a cube above a tilted plane. As the cube rotates, the cube's position vector is projected onto the plane and rendered as a line. using UnityEngine; public class ProjectOnPlaneExampleUpdate: MonoBehaviour { GameObject groundPlane; GameObject rotObject; LineRenderer line; void Start () { // Create the plane groundPlane = GameObject.CreatePrimitive(PrimitiveType.Plane); groundPlane.transform.Rotate(-30, 10, 0); // Create the item to rotate rotObject = GameObject.CreatePrimitive(PrimitiveType.Cube); rotObject.transform.position = new Vector3(5,5,0); line = rotObject.AddComponent<LineRenderer>(); } void Update() { // Set the rotation origin Vector3 origin = Vector3.zero; // Rotate the object above the plane rotObject.transform.RotateAround(origin, Vector3.up, 20 * Time.deltaTime); // Project the location of the cube onto the plane Vector3 projected = Vector3.ProjectOnPlane(rotObject.transform.position, groundPlane.transform.up); // Draw the projected vector as a line line.SetPosition(0, origin); line.SetPosition(1, projected); line.startWidth = 0.1f; } }
Additional Resources: Vector3.Project, Vector3.Reflect.

ProjectOnPlane(Vector3, Vector3)

Projects a vector onto a plane.
public static Vector3 ProjectOnPlane(in Vector3 vector, in Vector3 planeNormal)

Parameters

vector

The vector to project on the plane.

planeNormal

The normal which defines the plane to project on.

Returns

Type

Description

Vector3The vector that results from projection of
vector
on the plane.

Remarks

This method resolves the input vector onto the plane and returns the resulting vector. The new vector is orthogonal to
planeNormal
and parallel to the plane.
Note that
planeNormal
does not need to be normalized.
// This example rotates a cube above a tilted plane. As the cube rotates, the cube's position vector is projected onto the plane and rendered as a line. using UnityEngine; public class ProjectOnPlaneExampleUpdate: MonoBehaviour { GameObject groundPlane; GameObject rotObject; LineRenderer line; void Start () { // Create the plane groundPlane = GameObject.CreatePrimitive(PrimitiveType.Plane); groundPlane.transform.Rotate(-30, 10, 0); // Create the item to rotate rotObject = GameObject.CreatePrimitive(PrimitiveType.Cube); rotObject.transform.position = new Vector3(5,5,0); line = rotObject.AddComponent<LineRenderer>(); } void Update() { // Set the rotation origin Vector3 origin = Vector3.zero; // Rotate the object above the plane rotObject.transform.RotateAround(origin, Vector3.up, 20 * Time.deltaTime); // Project the location of the cube onto the plane Vector3 projected = Vector3.ProjectOnPlane(rotObject.transform.position, groundPlane.transform.up); // Draw the projected vector as a line line.SetPosition(0, origin); line.SetPosition(1, projected); line.startWidth = 0.1f; } }
Additional Resources: Vector3.Project, Vector3.Reflect.