Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Vector3.Project

Suggest a change

Success!

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.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Project(vector: Vector3, onNormal: Vector3): Vector3;
public static Vector3 Project(Vector3 vector, Vector3 onNormal);
public static def Project(vector as Vector3, onNormal as Vector3) as Vector3

Description

Projects a vector onto another vector.

To understand vector projection, imagine that onNormal is resting on a line pointing in its direction. Somewhere along that line will be the nearest point to the tip of vector. The projection is just onNormal rescaled so that it reaches that point on the line.

The function will return a zero vector if onNormal is almost zero.

An example of the usage of projection is a rail-mounted gun that should slide so that it gets as close as possible to a target object. The projection of the target heading along the direction of the rail can be used to move the gun by applying a force to a rigidbody, say.

	function Slide(target: Transform, railDirection: Vector3) {
		var heading = target.position - transform.position;
		var force = Vector3.Project(heading, railDirection);
		rigidbody.AddForce(force);
	}
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);
        rigidbody.AddForce(force);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Slide(target as Transform, railDirection as Vector3) as void:
		heading as Vector3 = (target.position - transform.position)
		force as Vector3 = Vector3.Project(heading, railDirection)
		rigidbody.AddForce(force)