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;