Switching Cameras

You are currently viewing Switching Cameras

I have the basic maze generation up and running, but I can’t really see it from the starting position. So I set off a mission that I originally thought would be simple – when I press a key, I want to switch to an overhead view of the maze.

I created a camera object in the world, and when I clicked it had a great top-down view. Great. Now all I have to do is add the key-swap. However, I didn’t stumble across any simple ‘view change’ options in my search. Instead, I found this function:

SetViewTargetWithBlend transitions the camera to another object in the world. Which gets the job done, but now I needed a way to get my camera object in the world into my code.

My first thought was to find it by the name I used in the editor, CameraOverhead. This is generally agreed to be poor practice, since objects are remade and renamed all the time. But in Unreal it is an especially bad idea – the “Item Label” is not the same as the Object Name, and the code could not find anything called “CameraOverhead” even though we are looking right at it in the editor.

So I got familiar with two other ways to get objects out of the world: Getting all actors of a certain class, and checking the tags of a given actor. The former allows you to search a much smaller selection of actors, which is good since this is quite slow.

After that the tags can be added simply in editor. This is slightly more resistant to re-naming, although I still have hard-coded searching for this new tag “overhead”. But bing bang boom our code is finding the overhead camera now, and the swap works nicely! I actually like the blend quite a bit, and it’s cool that the controls are still active – If I wanted, I could move the camera in closer and allow for overhead maze-solving.