お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。
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別のベクトルにベクトルを投影します
ベクトルの投影を理解するには、 onNormal
がその方向の直線上に静止していると想像してみてください。
ラインは vector
の先の位置から最も近い位置のどこかです。
投影は onNormal
に再スケーリングされライン上の位置に到達します。
/onNormal/ がほぼ0である場合、この関数は0ベクトルを返します。
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)