Rotating camera based on target's direction

I am building a camera system which meets the following requirements:

  1. Moves with the player, facing the player. The player model either faces left or right as to provide a side profile of the character.

  2. Upon entering a collider, the player may be rotated 90 degrees. (Imagine turning a corner in a hallway). The camera needs to swing or pivot around with the player as to maintain the view in requirement 1.

I am testing this using a cube on a plane. I am trying to navigate around each corner as to be able to circle the entire cube using 4 turns, and having the player always visible as defined above.

What is an optimal way of finding out which way the player object is facing so that I can set the distance of the camera to transform on the appropriate axis?

1 Answer

1

just put the camera where you want it in relation to the cube character your using and parent the camera to the player.

But if I do this and delete the player, say they lost, then the camera would be destroyed. Or am I missing something?

when you lose you need to load a new scene (a loosing screen) which has options to start again, go back to main menu etc whatever you like but yeah best off to do it this way. OR! you could have your script for your player turn on a "death" camera (just another camera that follows your player or has a wide angle of your level) that is by default switched off. (this isn't as easy to utilise if you want a restart level option though)

This is because Transform implements IEnumerable interface to return children. GameObject does not as it would not have any suitable logic for implementation.