unity3d - Unity3d:找到哪个游戏对象在前面
问题描述
我有两个gameObjects
A
和B
。他们rotated
在90 degrees
,这使它的local y axis
脸向前。
第一种情况
在这种情况下,local y position of B
领先于local y position of A
第二种情况
尽管它们的全局位置与第一种情况相同,但我们可以在这里观察到local y position of A
领先于local y position of B
.
我尝试使用A.transform.localPosition.y
andB.transform.localPosition.y
来查找哪个更大,但它不起作用。在这两种不同的情况下,我该怎么做才能找到哪个是最前面的?
解决方案
您可以比较Vector3.Dot(A.position, A.forward)
并Vector3.Dot(B.position, B.forward)
找到与他们的前锋相关的前锋。
具有较大点积的对象在前面,这适用于所有旋转,包括 3D 旋转。
您可以使用以下代码段自行测试:
// Assign these values on the Inspector
public Transform a, b;
public float RotationZ;
void Update() {
a.eulerAngles = new Vector3(0, 0, RotationZ);
b.eulerAngles = new Vector3(0, 0, RotationZ);
Debug.DrawRay(a.position, a.right, Color.green);
Debug.DrawRay(b.position, b.right, Color.red);
var DotA = Vector2.Dot(a.position, a.right);
var DotB = Vector2.Dot(b.position, b.right);
if (DotA > DotB) { Debug.Log("A is in front"); }
else { Debug.Log("B is in front"); }
}
推荐阅读
- vb.net - 我计算收据的 Visual Basic 程序(使用子例程和验证函数)不工作
- javascript - 在运行时在 Gatsby 中重新运行查询
- x86-16 - 段和偏移使用
- flutter - D/FlutterActivityAndFragmentDelegate(29168):没有提供首选的 FlutterEngine。为这个 FlutterFragment.Flutter 创建一个新的 FlutterEngine:
- asp.net-mvc - MVC does not render view if querystring is used
- sql-server - 来自 Microsoft SQL Server 的整理格式不佳的数据
- node.js - SequelizeDatabaseError:关系notice_opened_tbl的权限被拒绝
- python - 数组中的 lambda 找不到变量值
- python - 将 numpy 数组中的特定值转换为 NAN
- html - Sass 插值容器高度