经过一些实验后,我为角色的颈部添加了一个空(HeadCam)。此片段允许头部与纸板头部/相机同步旋转。
void LateUpdate() {
neckBone.transform.rotation = Camera.transform.rotation * Quaternion.Euler( 0,0,-90);
Camera.transform.position = HeadCam.transform.position;
}
当只有头部在-60°到60°的范围内旋转时,角色的手臂不应该移动,之后我想移动整个角色,手臂仍然可见。下面的方法有效,只要字符旋转不超过180°,之后字符翻转180°,我如何实现恒定旋转?
void LateUpdate() {
Quaternion camRot = Camera.transform.rotation * Quaternion.Euler( 0,0,-90);
neckBone.transform.rotation = camRot;
float yrot = camRot.eulerAngles.y;
float ydelta = 0;
if ( yrot < 300f && yrot > 180 ) {
ydelta = yrot - 300f;
}
if ( yrot > 60f && yrot < 180 ) {
ydelta = yrot - 60;
}
playerObj.transform.rotation = Quaternion.Euler(0, ydelta, 0);
Camera.transform.position = HeadCam.transform.position;
}
用于独立测试算法的java applet:https://github.com/3dbug/blender/blob/master/HeadCamRot.java
发布于 2015-06-09 06:13:56
最后,我找到了一个解决方案:
private float bodyRot = 0F;
private float FOV = 70f;
void LateUpdate() {
if ( neckBone != null ) {
Quaternion camRotQ = CameraFacing.transform.rotation * Quaternion.Euler( 0,0,-90);
neckBone.transform.rotation = camRotQ;
float camRot = camRotQ.eulerAngles.y;
float delta = camRot- bodyRot;
if ( delta > 180 ) {
delta -= 360;
}
if ( delta < -180 ) {
delta += 360;
}
if ( Math.Abs(delta) > FOV ) {
if ((delta > FOV || delta < -180) && delta < 180) {
bodyRot = camRot - FOV;
}
delta = camRot- bodyRot;
if ((delta < FOV || delta > 180 ) ) {
bodyRot = camRot + FOV;
}
}
playerObj.transform.rotation = Quaternion.Euler(0, bodyRot, 0);
CameraFacing.transform.position = cameraMount.transform.position;
}
}
https://stackoverflow.com/questions/30654290
复制相似问题