Unity中的朝向和Quaternion类总结

1. transform.LookAt(Camera.main.transform.position);   //transform的Z轴朝向Camera.main.transform.position

2.     Enemy朝向Camera.main在某个轴(Z轴)上,不同与LookAt()在于LookAt的朝向可能会导致Enemy的倾斜(如图)

        

       Rotation newRotation = Quaternion.LookRotation(Camera.main.transform.position - transform.position).eulerAngles;  
        newRotation.x = 0;
        newRotation.z = 0;
        transform.rotation = Quaternion.Euler(newRotation);

3.  SmoothLookAtPlayerOnOneAxis: 效果类似2,只是在Enemy转向的时候用球型插值:      

    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);

4. RotateToPlayerWithAMaximumSpeed:效果同3,也就是在朝向旋转的时候使用一个最大速度和预计平滑的时间(Mathf类函数可操靠此博客,介绍的较详细: https://blog.csdn.net/jk823394954/article/details/52431615

_transform.rotation = Quaternion.Euler(angles.x, Mathf.SmoothDampAngle(angles.y, newRotation.y, ref _velocity,            minimumTimeToReachTarget, maximumRotateSpeed), angles.z);   

5.略

6.  //Quaternion相乘是右乘,先进行最右边的quaternion,然后才进行左边的quaternion;

_transform.rotation = _transform.rotation * Quaternion.AngleAxis(Time.deltaTime * 20 * _rotatingFactor, Vector3.up);

 

7.         _transform.forward = _cameraTransform.forward;    //_transform的旋转和_cameraTransform保持一致

 

8.      //_transform的forward和_cameraTransform保持一致,必须右乘_transform.rotation才能保证一致

       _transform.rotation = Quaternion.FromToRotation(_transform.forward, _cameraTransform.forward) * _transform.rotation;

 

9.      Physics.SphereCast() :   

        以下代码只是展示不同方式的旋转:

        1. _transform.rotation = _transform.rotation * Quaternion.AngleAxis(Time.deltaTime * 20 * _rotatingFactor, Vector3.up); //旋转方式;

       //Adjust forward To Point to Cemera  ,使 _transform的forward朝向_cameraTransform的forward,当相机旋转的时候  _transform始终保视野垂直的状态;

     2.// 使_transform的forward始终朝向_camera.forward;

      _transform.rotation = Quaternion.FromToRotation(_transform.forward, _cameraTransform.forward) * _transform.rotation;

     3. _transform.rotation = Quaternion.LookRotation(-_cameraTransform.forward, Vector3.up) * _pointUpAtForward; //Z轴沿着-_cameraTransform.forward, y轴尽量靠近Vector3.up;

 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值