1

我正在开发一个包含第一人称角色控制器的 Unity 3D 游戏。我正在尝试旋转其顶部的主摄像机,以模拟角色沿直线行走时左右转动头部。我的问题是我的旋转相机的代码似乎干扰了它作为父对象的角色控制器对象的旋转。

这是我的代码。这个问题似乎发生在最后一行。

void Update () {
    //Send screen image to controller
    StartCoroutine(ScreenshotEncode());

    //the camera is parented to the Character Controller game object.  
    //Dude
    //  +-Capsule
    //  +-MainCamera

    //This code come directly from the Unity script manual
    DudeBase.transform.Rotate(0, Input.GetAxis("Horizontal") * flrBaseMaxRotateSpeed, 0);
    //get the vector the base in pointing toward
    vctBaseDir = DudeBase.transform.TransformDirection(Vector3.forward);
    //get the speed of the base
    fltBaseSpeed = fltBaseMaxSpeed * Input.GetAxis("Vertical");
    //apply speed and direction to the character controller
    controller.SimpleMove(vctBaseDir * fltBaseSpeed);

    //The camera controller comes from the device in degress in the form of a string.
    //get the heading the controller is pointing toward
    fltCamControllerDir = ParseControllerData(strCamControllerData);
    //get the heading of the base
    fltBaseDir = Mathf.Atan2(vctBaseDir.x, vctBaseDir.z);
    //we want to point the camera the angle as the controller.
    //since the camera is sitting on top of and is parented to the base, it needs to offset by the bases heading
    fltCamDir = fltBaseDir + fltCamControllerDir;
    //set the camera's y axis angle
    MainCam.transform.eulerAngles = new Vector3(0, fltCamDir, 0);  // <=== issue seems to be here
    //if i comment out the last line, the character controller moves and rotates as expected.
    //if i uncomment this line, the camera moves on top of the character controller as expected. But,
    //the character controller itself no longer rotates.
}

如何在不影响其父对象的情况下旋转相机对象?

4

1 回答 1

0

Jerdak 的评论是正确的答案:

向您的第一人称添加一个新的空子对象并将其命名为head. Make the camera a child ofhead`。

当玩家移动或旋转时,head物体会移动或旋转,并带上相机。如果您想独立于机身移动或旋转相机,只需在head不接触机身的情况下移动/旋转即可。

于 2013-01-12T01:17:52.707 回答