有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当角色的头部已经旋转了60°时,角色的身体如何能够持续旋转?

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

void LateUpdate() {
    neckBone.transform.rotation = Camera.transform.rotation *  Quaternion.Euler( 0,0,-90);
    Camera.transform.position = HeadCam.transform.position;
}

enter image description here

当只有头部在-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;
}

enter image description here

用于测试独立算法的java小程序:https://github.com/3dbug/blender/blob/master/HeadCamRot.java


共 (2) 个答案

  1. # 1 楼答案

    一种可能的解决办法是:

    // 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角度不会超过最大角度。此外,即使如此,如果需要的话,在上面的函数中添加该限制也是非常简单的

    希望能有帮助

  2. # 2 楼答案

    最后,我找到了一个解决方案:

    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;
        }
    }