Vector Unity3d OnTriggerCenter碰撞方向检测

Vector Unity3d OnTriggerCenter碰撞方向检测,vector,unity3d,collision-detection,euler-angles,Vector,Unity3d,Collision Detection,Euler Angles,好的,我认为这个概念应该很简单。然而,答案却在我眼前。编辑背景。“其他”对象和带有此代码的游戏对象都有作为触发器的球体碰撞器,此游戏对象有一个用于移动的CharacterController,但都没有刚体。我的目标是让它(玩家对象,像吃豆人一样自动移动)杀死它遇到的东西,但只有当它面对它们时,否则,它会受到伤害。我的问题是如何判断它是否面对目标。要完成图片,当玩家和NPC仅沿X轴和Y轴移动时,moveDirection可以是以下之一(Vector3.Up、.Down、.Left或.Right)(

好的,我认为这个概念应该很简单。然而,答案却在我眼前。编辑背景。“其他”对象和带有此代码的游戏对象都有作为触发器的球体碰撞器,此游戏对象有一个用于移动的CharacterController,但都没有刚体。我的目标是让它(玩家对象,像吃豆人一样自动移动)杀死它遇到的东西,但只有当它面对它们时,否则,它会受到伤害。我的问题是如何判断它是否面对目标。要完成图片,当玩家和NPC仅沿X轴和Y轴移动时,moveDirection可以是以下之一(Vector3.Up、.Down、.Left或.Right)(带有3D游戏对象的正交摄影机)

~老办法

    void OnTriggerEnter (Collider other)
    {

        if (other.gameObject.tag == "Human") {
            RaycastHit hit;
            if (Physics.Raycast (transform.position, moveDirection, out hit)) {
                StartCoroutine (eatWithBlood (1, other.gameObject.GetComponent<NPCController> ()));
            } else {
                TakeDamage (other.gameObject.GetComponent<NPCController> ().attack);
            }

        } 
    }
void ontriggenter(碰撞器其他)
{
if(other.gameObject.tag==“Human”){
雷卡斯特击中;
if(Physics.Raycast(transform.position、moveDirection、out-hit)){
start例程(eatWithBlood(1,other.gameObject.GetComponent());
}否则{
TakeDamage(other.gameObject.GetComponent().attack);
}
} 
}
我还尝试了下面的代码,效果稍微好一点。数学计算不太正确,所以即使角度在45到-45之间,玩家仍然会“晕眩”并受到伤害。新方法已预配置为在需要其他变量以进行更精确计算时进行扩展。faceDirection变量是up=0、right=1、down=2、left=3,它们也对应于它们的向量3。up、left、down和right队列

~新方法

void OnTriggerEnter (Collider other)
    {

        if (other.gameObject.tag == "Human") {
            float angle = Vector3.Angle (transform.forward, other.gameObject.transform.position);
            Debug.Log ("Angle: " + angle + "; my forward: " + faceDirection + "; other forward: " + other.gameObject.GetComponent<NPCController> ().faceDirection + " ... " + Time.time);
            if (IAteHim (faceDirection, other.gameObject.GetComponent<NPCController> ().faceDirection, angle)) {
                other.gameObject.GetComponent<NPCController> ().EatMe ();
                StartCoroutine (eatWithBlood (1, other.gameObject.GetComponent<NPCController> ()));
            } else {
                GameObject poof = PoofPooler.poof.GetPooledObject ();
                if (poof != null) {
                    poof.transform.position = Vector3.Lerp (transform.position, other.gameObject.transform.position, 0.5f);
                    poof.GetComponent<PoofControl> ().reactivated = true;
                    poof.SetActive (true);

                }
                other.gameObject.GetComponent<NPCController> ().MakeInvincible ();
                TakeDamage (other.gameObject.GetComponent<NPCController> ().attack, 2.0f);
            }


        }
    }

    bool IAteHim (int myDirection, int otherDirection, float angle)
    {
        if (angle > -45 && angle < 45) { // I'm facing toward yummies
            return true;
        } else {
            return false;
        }

    }
void ontriggenter(碰撞器其他)
{
if(other.gameObject.tag==“Human”){
浮动角度=矢量3.角度(transform.forward,other.gameObject.transform.position);
Log(“角度:+Angle+”;“我的转发:+faceDirection+”;“其他转发:+other.gameObject.GetComponent().faceDirection+“…”+Time.Time);
if(IAteHim(faceDirection,other.gameObject.GetComponent().faceDirection,angle)){
other.gameObject.GetComponent().EatMe();
start例程(eatWithBlood(1,other.gameObject.GetComponent());
}否则{
GameObject poof=PoofPooler.poof.GetPooledObject();
如果(poof!=null){
poof.transform.position=Vector3.Lerp(transform.position,other.gameObject.transform.position,0.5f);
poof.GetComponent().reactivated=true;
poof.SetActive(真);
}
other.gameObject.GetComponent().MakeInvincible();
TakeDamage(other.gameObject.GetComponent().attack,2.0f);
}
}
}
bool-IAteHim(int-myDirection,int-otherDirection,float-angle)
{
如果(角度>-45&&angle<45){//我正对着尤米斯
返回true;
}否则{
返回false;
}
}
更改

float angle = Vector3.Angle (transform.forward, other.gameObject.transform.position);

Vector3 direction = other.gameObject.transform.position - transform.position;
float angle = Vector3.Angle (transform.forward, direction);