The latest game prototype ‘Transmutation’ requires the player to be able to control 3 playable characters – but not simultaneously. As it is, the player starts playing the character on the right, we’ll call him Mr. Broom. The other two playable characters Pots and Meat are currently sitting idle. There needs to be a way to switch playable characters – this blog will cover how I made it happen.
An appropriate place to handle the switching would be the GameController. It will end up having multiple scripts, one of which I will set up to only handle the character switching. This script is called ‘CharacterSwitcher’. In order for the character switcher to be able to switch characters it’s going to need to know:
- What three playable characters we have in the game.
- What camera or how many camera’s we are using.
- The position of the camera’s or the positions of where an individual camera will move to.
There are only ever going to be three playable characters, but how many camera’s are we going to use and why? Three cameras and always have them be in the positions they need to be and just activate or deactivate them when needed? Or have a single camera and move it to desired locations?
I chose for the game to only operate on a single camera and move to the position it needs to. Purely because of wanting to always have a consistent view on the game world. Plus it would enable us to do something like an ‘XCOM’ game. Lerp the camera between the player’s its switching between except our camera is a fixed 3rd person perspective from behind the playable character.
So now that I’ve decided on a single camera I’ll set up the CharacterSwitcher script to know what it needs.
The camera spots are empty game objects that are a child of each of the playable characters. They each have the exact same local position behind each character. Now that the GameController know’s what it needs and has them – I need to devise a way to switch between them. Speaking of switching, why not use a switch statement? The GameController will need to know which character is currently selected and which one it needs to go to.
In Start(), tell the GameController we are controlling character one. Everything is set up, now it just needs a way to actually switch. I made switching right E and switching left Q for the time being until we learn how to set up the game with a controller. I called these methods straight in update even though they didn’t exist. On many occasions I’ve created complex methods that never work because I create the system and never call it. This time though, I’m going to completely avoid that.
The switch statement checks which character is currently selected – and as of start, it’s playable character #1. When ‘E’ is pressed it switches to playable character #2. The <CharacterController> script is the script attached to the playable character that allows the player to control it (if the name wasn’t obvious enough). Each time a switch happens, it gets the current playable character – deactivates its control script so it can’t run around, moves the cameras position to the next playable characters camera position and changes the switch statement to the current character.
*video of Switching*
It’s not Lerping as of yet, but that’s a task for another day, it currently serves it’s soul purpose of switching.
Until next time –