2D怪物朝向移动



protected void Move()
    {
        transform.right = -(Role.position - transform.position).normalized;//怪物右面永远背对人物

        transform.Translate(-transform.right * m_guaiSpeed, Role);//往左方向移动丶朝向人物

        Turn();

    }


//怪物面向贴图替换(spriteAnim)
    public UILabel m_lab = null;
    void  Turn()
    {

        string text = "";
        float angle = Vector3.Angle(-RoleMgr.GetInstance().CurRole.transform.up, -transform.up);//0-180
        Vector3 v = Role.position - transform.position;
        //背面
        if(angle>=45 && angle<=135 && v.y>0)
        {
            text="背面";
        }
        else if (angle >= 45 && angle <= 135 && v.y < 0)
        {
            text="正面";
        }
        else if (angle >= 0 && angle <= 45 && v.x<0)
        {
            text="侧面";
        }
        else if (angle >= 135 && angle <= 180 && v.x > 0)
        {
            text="反侧";
        }

        m_lab.text = text;
    }

你可能感兴趣的:(2D怪物朝向移动)