お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。
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ベクトルの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!"); } }
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public Transform other; public float closeDistance = 5.0F; void Update() { if (other) { Vector3 offset = other.position - transform.position; float sqrLen = offset.sqrMagnitude; if (sqrLen < closeDistance * closeDistance) print("The other transform is close to me!"); } } }
import UnityEngine import System.Collections public class ExampleClass(MonoBehaviour): public other as Transform public closeDistance as float = 5.0F def Update() as void: if other: offset as Vector3 = (other.position - transform.position) sqrLen as float = offset.sqrMagnitude if sqrLen < (closeDistance * closeDistance): print('The other transform is close to me!')