Version: 5.4 (switch to 5.5b)
言語English
  • C#
  • JS

Script language

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

Vector3.sqrMagnitude

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 var sqrMagnitude: float;
public float sqrMagnitude;

Description

ベクトルの 2 乗の長さを返します(読み取り専用)

ベクトル 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!"); } } }