zoukankan      html  css  js  c++  java
  • unity Mathf.Atan2()

    using UnityEngine;
    
    public class TestGetAnget : MonoBehaviour
    {
        public Transform m_target1;
        public Transform m_target2;
        public Transform target;
        public Transform target1;
    
        void Update()
        {
            GetAnglev3();
            GetAngle(m_target1, m_target2);
        }
    
        void GetAnglev3()
        {
            Vector3 relative = target1.InverseTransformPoint(target.position);//将target.position转换为target1的局部坐标
            float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;//rotationY等于0时,面朝向z轴,所以将z轴当作atan2里的x,x轴当作atan2里的y
            target1.Rotate(0, angle, 0);//转换局部坐标时已经把target1的旋转角计算在内,这里的angle是rotationY的相对旋转角
        }
    
        void GetAngle(Transform target1,Transform target2)
        {
            Vector3 dir = target1.position - target2.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            m_target1.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
    }
    

    Mathf.Atan2(float y,float x)
    如果y和x都为正数,得到的结果也一定是正数。
    计算rotationY时,使用Mathf.Atan2(+z,+x),得到的结果也一定是正数。
    从俯视图上看rotationY是顺时针旋转的,而+z和+x又一定是在右上方,因此得到的结果是相反的,所以rotationY=-Mathf.Atan2(z,x)才能得到正确的y轴旋转角
    或者使用Quaternion.AngleAxis(float angle,Vector3 axis)进行转换。

    float rotationY=Quaternion.AngleAxis(Mathf.Atan2(z,x)*Mathf.Rad2Deg, Vector3.down).eulerAngles.y;//得到正确的rotationY
    //或
    float rotationY=-Mathf.Atan2(z,x)*Mathf.Rad2Deg;//取负,得到正确的rotationY
    

    将transform.rotationY朝向指定点的最简便方法(只设置rotationY):

    //targetPosition:目标位置
    //transform:人物的Transform
    
    //1.俯视图中人物朝向z轴。
    Vector3 relative=transform.InverseTransformPoint(targetPosition);
    float rotationY=Mathf.Atan2(relative.x,relative.z)*Mathf.Rad2Deg;// atan2(x,z);
    transform.Rotate(0,rotationY,0);
    //2.俯视图中人物朝向x轴。
    Vector3 relative=transform.InverseTransformPoint(targetPosition);
    float rotationY=-Mathf.Atan2(relative.z,relative.x)*Mathf.Rad2Deg;// -atan2(z,x);
    transform.Rotate(0,rotationY,0);
    
  • 相关阅读:
    词汇小助手V1.0——一款跨平台词频统计和外语学习工具
    穿越狙击V1.0
    词汇小助手V1.4——加入单词测试功能
    词汇小助手V1.2——可以显示英语单词的国际音标
    词汇小助手官方网站发布了
    强大的云存储与应用管理工具DzzOffice1.0 Beta(大桌子办公)发布下载 大桌子
    <!DOCTYPE>前加有<! xxx >注释在IE中引发的bug
    去笔试腾讯的前端实习生,题目太尼玛坑爹了
    IE中的布局BUG和一些可以避开的BUG
    removeClass,addClass的原生JS代码
  • 原文地址:https://www.cnblogs.com/kingBook/p/12793578.html
Copyright © 2011-2022 走看看