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);
    
  • 相关阅读:
    用js实现cookie的读、写、全部删除和删除指定cookie值的删除---源码
    JS手机号码格式验证
    vuex
    解决“此图片来自微信公众平台未经允许不可引用”的方法
    二叉树
    剑指 Offer 10- II. 青蛙跳台阶问题
    logrotate处理Gunicorn日志
    Linux日志切割神器logrotate原理介绍和配置详解
    Jenkins任务启动的后台进程被自动kill
    Flex、Grid、媒体查询实现响应式布局
  • 原文地址:https://www.cnblogs.com/kingBook/p/12793578.html
Copyright © 2011-2022 走看看