Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Vector3.sqrMagnitude 长度平方
# [Vector3](Vector3.html).sqrMagnitude 长度平方 var *sqrMagnitude* : float *Description* 描述 Returns the squared length of this vector (Read Only). 返回这个向量的长度的平方(只读)。 Calculating the squared magnitude instead of the [magnitude](Vector3.magnitude.html) is much faster. Often if you are comparing magnitudes of two vectors you can just compare their squared magnitudes. 计算长度的平方而不是 [magnitude](Vector3.magnitude.html) 是非常快的。如果你是比较两个向量的长度差,你可以比较他们的平方长度。 > 向量的长度是用勾股定理计算出来,计算机计算两次方和开根的运算量比加减法要费时的多。所以如果是想比较两个向量的长度,用 sqrMagnitude 可以快出很多。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Transform other; public float closeDistance = 5.0F; void Update() { if (typeof(other)) { float sqrLen = other.position - transform.position.sqrMagnitude; if (sqrLen < closeDistance * closeDistance) print("The other transform is close to me!"); } } } ``` ``` // detects when the other transform is closer than closeDistance // this is faster than using Vector3.magnitude //当其他变换比closeDistance更近时检测 //这比使用Vector3.magnitude更快 var other : Transform; var closeDistance = 5.0; function Update() { if (other) { var sqrLen = (other.position - transform.position).sqrMagnitude; // square the distance we compare with //我们比较距离的平方 if( sqrLen < closeDistance*closeDistance ) print ("The other transform is close to me!"); } } ``` 参见: [magnitude](Vector3.magnitude.html).
da
2022年5月23日 21:53
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码