Isometric Camera Angle Control in Unity
An update on my isometric side-project
Today I’m taking a break from my usual routine to work on a side-project. My goal for this jaunt was to give the player the option of viewing the scene from one of four different isometric angles. I also wanted a chance to play with the new isometric building block assets I picked up from the Unity Asset store.
First, I constructed a simple environment for my player to explore.
Then I set up my Cinemachine Virtual Cameras.
Each camera is set to follow the player, with its x-rotation at 32deg, Body set to “Framing Transposer” and Aim set to “Do Nothing”
The y-rotation of each camera will be the four diagonal angles: 45, 125, 225, and 305
Next we need to script the actions for the input system. I’ve added two new actions to the Player Action Map:
Next, I need to create a serialized array in the player class and assign the cameras to it, as well as an index variable. I’ll also need to include the two new action functions from Controls.IPlayerActions in the Player Script. For each, I’ll do the following:
If the button is pressed and can rotate is true
… set can rotate to false (so the script only runs once per button press)
…change current camera priority to 10
…increment/decrement camera index
… if the camera index is outside of value, loop around to valid index
…change new current camera priority to 20
if the action has been performed
…set can rotate to true
In C#, here’s our script:
Now when I press E, the camera to the right is raised to highest priority and we transition to that virtual camera. Pressing Q sends us back the other direction.
Once controls are set up, we need to adapt the function that rotates our movement vectors. The old function was designed for a fixed camera. Here’s what it looked like:
That Quaternion.Euler(0, 45.0f, 0) is what needs to change — instead of a hard-coded number, I need the rotation information from each camera to inform the conversion function as to what degree of rotation is necessary.
The reworked code first finds the Euler angles for the current camera’s rotation. Then the rotation variable can use the isolated y-axis information to know how to rotate the vectors so that “up” is always “up” no matter which way the camera turns.
Here it is in action:
Tomorrow I really will tell you about whatever it was I said I was going to write about last time. Thanks for digressing with me.