问题描述
我寻找最好的方法来检测 GO(立方体)的哪个位置朝上。 我的研究使我想到了点积。 我知道我想做什么,但我想我的 c# 技能太糟糕了..
基本上我只想找到例如 x 旋转的点积。 如果它的值介于 0,9 到 1 之间,则一个站点朝上,对于 -0,9 到 -1,另一个站点则是所有轴,依此类推。
解决方法
在 Unity 的文档中,您有以下 example。您只需稍微调整一下即可实现您的需求。
// detects if other transform is behind this object
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Update()
{
if (other)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = other.position - transform.position;
if (Vector3.Dot(forward,toOther) < 0)
{
print("The other transform is behind me!");
}
}
}
}