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 に答える