Project(Vector3, Vector3)
Projects a vector onto another vector.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
Parameters
Returns
Type | Description |
|---|---|
| Vector3 | The vector that results from the projection of |
Remarks
The resulting projection is the amount of that points in the same direction as .
vectoronNormal
Diagram showing a vector projected onto the vector . The projected vector extends past in the same direction because the magnitude of in the direction of is greater than the magnitude of . The result points to the point along the infinite line in the direction of that's closest to the point specified by .
onNormalonNormalvectoronNormalonNormalonNormalvectorThe function returns a zero vector if is almost zero. doesn't need to be a normalized vector.
onNormalonNormalYou can use projection to work out the closest point along a line to a target vector. This information can be useful for moving or orienting items in the direction of a moving target.
Examples
using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Slide(Transform target, Vector3 railDirection) { Vector3 heading = target.position - transform.position; Vector3 force = Vector3.Project(heading, railDirection); GetComponent<Rigidbody>().AddForce(force); }}