Unity 3d GO 方向

问题描述

我寻找最好的方法来检测 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!");
            }
        }
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...