言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

Vector3.sqrMagnitude

public var sqrMagnitude: float;

Description

ベクトルの2乗の長さを返します (Read Only)

ベクトル v の大きさは Mathf.Sqrt(Vector3.Dot(v, v)) によって計算されます。しかし、 Sqrtの計算はとても複雑で通常の計算よりも実行に 時間がかかってしまいます。 magnitude よりも2乗の大きさの計算のほうがはるかに高速です - 計算は基本的に遅いSqrtを呼び出しません。 簡単な距離を比較するためにベクトルの大きさを使用する場合、同じ結果が得られることから、 2乗した大きさから同じように2乗した距離の比較を行うことが出来ます。 See Also: magnitude.

	// detects when the other transform is closer than closeDistance
	// this is faster than using Vector3.magnitude

	var other : Transform;
	var closeDistance = 5.0;
	
	function Update() {
		if (other) {
			var offset = other.position - transform.position;
			var sqrLen = offset.sqrMagnitude;
			// square the distance we compare with
			if( sqrLen < closeDistance*closeDistance )
				print ("The other transform is close to me!");
		}
	}