- Home /
Orient GameObject rotation to rotation of Main Camera
Heirarchy: Camera (main) > Player (GameObject, Empty) > PlayerModel (Rigidbody)
Code:
void Update () {
camForward = playerCam.transform.forward;
//this.gameObject.transform.Translate(0, 0, speed * Time.deltaTime);
SetUpControls();
//Gets the direction being pushed
Vector3 direction = GetDirectionVector();
//Inverted direction
Vector3 lookDirection = direction;
lookDirection.z += 2.5f;
transform.rotation = playerCam.transform.rotation;
UseBrakes();
MoveInDirection(playerShip, direction, speed);
KeepShipInBounds();
RotateTowardsDirection(playerShip, lookDirection, 120.0F);
Debug.Log(shipPositionToCamera.ToString());
}
The image below is from the camera's perspective shortly after the game has been started. The world positions of the camera and player are still at their original points on their x and y axes respectively, only having moved a little along their z axis.
The next image is from the camera's perspective after the camera has rotated a bit (rail shooter) along its path.
As we can see, the ship has maintained its original rotation, even though each frame it should be updating its rotation to the camera's rotation. (transform.rotation = playerCam.transform.rotation)
My question, is "how do I orient a GameObjects rotation (in this case, the star ship) to the rotation of the main camera?"
(Please note that the code RotateTowardsDirection(playerShip, lookDirection, 120.0F);does not affect anything. It can be commented out and the issue persists. The method I have made for it does not lock the ship into this view, as the "lookDirection" value is always out in front of the camera.)
since the player is a child of the camera already, why are you even rotating it?