一人称キャラクターコントローラーを含む 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.
}
親オブジェクトに影響を与えずにカメラ オブジェクトを回転するにはどうすればよいですか?