Quaternion.LookRotation和lookat

Quaternion.LookRotation(v3 v)的参数是一个向量,是让物体z轴和这个向量重合

Quaternion.LookRotation(v3 v1,v3 v2)前一个参数对应物体z轴,后一个对应Y轴

lookat的参数是一个点,是让物体Z轴指向这个点

利用这一点可以实现一个简单的移动

void Move()
    {
        float horizontal = Input.GetAxis("Horizontal");//获取水平偏移量(x轴)
        float vertical = Input.GetAxis("Vertical");    //获取垂直偏移量(z轴)
        //将水平偏移量与垂直偏移量组合为一个方向向量
        Vector3 direction = new Vector3(horizontal, 0, vertical);
        //判断是否有水平偏移量与垂直偏移量产生
        if (direction != Vector3.zero)
        {
            //将游戏对象的z轴转向对应的方向向量
            //            transform.rotation = Quaternion.LookRotation(direction);
            //对上一行代码进行插值运算则可以将转向表现得较平滑
            B.rotation = Quaternion.Lerp(B.rotation, Quaternion.LookRotation(direction), 0.3f);
            //将游戏对象进行移动变换方法则可以实现简单的物体移动
            B.Translate(Vector3.forward * 5 * Time.deltaTime);
        }
    }

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/85247095