Java 字符';当它的头部已经转动60°;`时,它的身体会持续旋转吗?

Java 字符';当它的头部已经转动60°;`时,它的身体会持续旋转吗?,java,c#,unity3d,google-cardboard,Java,C#,Unity3d,Google Cardboard,经过一些实验后,我给角色的颈部添加了一个空的(头部摄像头)。 此片段允许头部与CardboardHead/Camera同步旋转 void LateUpdate() { neckBone.transform.rotation = Camera.transform.rotation * Quaternion.Euler( 0,0,-90); Camera.transform.position = HeadCam.transform.position; } 当只有头部在-60°到

经过一些实验后,我给角色的颈部添加了一个空的(头部摄像头)。 此片段允许头部与CardboardHead/Camera同步旋转

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;
}
void LateUpdate(){
四元数camRot=Camera.transform.rotation*四元数.Euler(0,0,-90);
neckBone.transform.rotation=camRot;
float yrot=camRot.eulerAngles.y;
浮动ydelta=0;
如果(yrot<300f&&yrot>180){
ydelta=yrot-300f;
}
如果(yrot>60华氏度和&yrot<180华氏度){
ydelta=yrot-60;
}
playerObj.transform.rotation=四元数.Euler(0,ydelta,0);
Camera.transform.position=HeadCam.transform.position;
}


用于独立测试算法的java小程序:

一种可能的解决方案是:

// Transform of the full body of the character.
Transform body;
// Transform of the head (child of |body| component).
Transform head;
// Maximum delta angle in degrees.
float maxAngle = 60.0f;

void RotateCharacter(Quaternion target) {
  // Rotate head as much as possible without exceeding the joint angle.
  head.rotation = Quaternion.RotateTowards (body.rotation, target, maxAngle);
  // Complete the remainder of the rotation by body.
  body.rotation = target * Quaternion.Inverse (head.localRotation);
}
请记住,您可能需要事先限制非水平旋转,也就是说,我假设给定的通过旋转的x&z角度不会超过maxAngle。此外,即使如此,如果需要的话,在上面的函数中添加该限制也是非常简单的


希望能有所帮助。

我终于找到了解决方案:

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;
    }
}
private float bodyRot=0F;
专用浮球FOV=70f;
void LateUpdate(){
if(颈骨!=null){
四元数camRotQ=CameraFacing.transform.rotation*四元数.Euler(0,0,-90);
neckBone.transform.rotation=camRotQ;
float camRot=camRotQ.eulerAngles.y;
浮动增量=camRot-bodyRot;
如果(增量>180){
delta-=360;
}
如果(增量<-180){
delta+=360;
}
如果(数学绝对值(增量)>视野){
如果((三角洲>视野| |三角洲<-180)和三角洲<180){
bodyRot=camRot-视野;
}
delta=camRot-bodyRot;
如果((delta180)){
bodyRot=camRot+视野;
}
}
playerObj.transform.rotation=四元数.Euler(0,bodyRot,0);
CameraFacing.transform.position=cameraMount.transform.position;
}
}

谢谢,应该使用什么作为目标?目前,我从纸板相机上读取旋转,并以此为基础进行计算。在这种情况下,纸板相机的方向(Transform.rotation)应该是目标。当然,如果你的角色模型有一些初始方向,也就是说,与相机的初始方向不同,你也应该考虑到这一点来匹配它的最终旋转。再次感谢你,这可能需要更多的时间才能正确。当我找到解决方案时,我会把它贴在这里。问题可以归结为Units在0和360°旋转时的行为,因此计算时需要考虑到这一点四元数。RotateTowards`在某些情况下可能非常有用。难道你不能使用一个更容易实现此目的的动画吗?@leAthlon这是一个虚拟现实应用程序,旋转必须根据相机的移动实时应用。基本上,当你往下看的时候,你会看到你自己。我不确定虚拟现实的东西——也许unity5 Prefables的mouselook脚本可以帮助你啊,我明白了。呃,我没有,但现在我有了。