美文网首页Unity进阶技术
unity 总结夹角的运算和Vector3.Dot的使用

unity 总结夹角的运算和Vector3.Dot的使用

作者: WOTTOW | 来源:发表于2019-03-20 19:02 被阅读0次

效果图:


angle.gif

计算夹角原理:
方法一:利用四元素的LookRotation,可以获取旋转的四元素值,也可以用四元素中带的Angle,计算出夹角

方法二:利用三维的Angle计算夹角

方法三:利用数学函数计算,使用到了Vector3.Dot计算点乘,
然后使用 Mathf.Acos弧度,弧度再转换成角度。

方向判断原理:
只需运用点乘即可。

代码:

//旋转角度
    private void GetAngleA()
    {
        Vector3 targetDir = target.position - transform.position;
        Quaternion roation = Quaternion.LookRotation(targetDir);
        float angle =Quaternion.Angle(transform.rotation, roation);
        print("角度A:"+angle);
    }

    //旋转角度
    private void GetAngleB()
    {
        Vector3 targetDir = target.position - transform.position;
        //lhs   rhs
        float angle = Vector3.Angle(targetDir, transform.forward);
        print("角度B:" + angle);
    }

    private void GetAngleC()
    {
        Vector3 targetDir = target.position - transform.position;
        Vector3 playerForward = target.rotation * transform.forward;
        float angle = Mathf.Acos(Vector3.Dot(playerForward.normalized, targetDir.normalized))* Mathf.Rad2Deg;
        print("角度C:" + angle);
    }

    //判断左右
    private void Getdrection()
    {
        Vector3 targetDir = target.position - transform.position;
        float dir = Vector3.Dot(targetDir.normalized,transform.right);
        if (dir > 0)
        {
            print("右边");
        }
        else
        {
            print("左边");
        }
    }

相关文章

  • unity 总结夹角的运算和Vector3.Dot的使用

    效果图: 计算夹角原理:方法一:利用四元素的LookRotation,可以获取旋转的四元素值,也可以用四元素中带的...

  • unity 计算夹角

    物体转向,在unity也比较常用。最近在使用时发现有点生疏了。在这里复习复习。

  • Unity 数学

    Unity3D中, Vector3.Dot 表示求两个向量的点积; Vector3.Cross 表示求两个向量的叉...

  • 点成叉乘 Unity

    在Unity3D中,Vector3.Dot表示求两个向量的点积;Vector3.Cross表示求两个向量的叉积。 ...

  • Unity3d数学基础之向量

    这只是基础的一些数学知识,后面会为大家整理一些,unity中如何使用向量,向量在unity中的各种算法及其运算法则...

  • 两整数之和(不使用+、-)

    (力扣371题)思路:说实话看到这个题没啥思路,最后只能总结,无进位加法使用异或运算计算得出,进位结果使用与运算和...

  • Unity Attribute的使用总结

    http://www.ithao123.cn/content-8813175.html

  • Unity游戏项目性能优化总结

    概述 本文就Unity游戏项目性能优化作出了总结。包括Profile工具、Unity使用、机制设计、脚本编写等方面...

  • JS1-3

    num++是先使用num的值,而后进行自增运算。 ++num是先进行自增运算,再被使用值。 Math的方法总结: ...

  • Unity游戏项目性能优化总结

    概述本文就Unity游戏项目性能优化作出了总结。包括Profile工具、Unity使用、机制设计、脚本编写等方面内...

网友评论

    本文标题:unity 总结夹角的运算和Vector3.Dot的使用

    本文链接:https://www.haomeiwen.com/subject/bcwtvqtx.html