Rotating camera

Hi folks,

I wanted to have the camera rotate so that the planet center would always be straight down. I realized that I could only rotate the camera by a given amount of degrees, not set an absolute angle. This made things a bit harder, so I thought I would share how I did it.

 //As center of planet is in (0, 0), it is enough to fetch the current position of the camera to get the correct angle.
 Vector2 shipPlanetVector = new Vector2(m_camera.position.x, m_camera.position.y);
 //Fetch vector up for camera. The angle between these two is how much we need to rotate the camera.
 Vector2 cameraUp = new Vector2(m_camera.up.x, m_camera.up.y);
 m_camera.rotate(cameraUp.angle() - shipPlanetVector.angle());

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s