Thursday, November 27, 2014

Unreal Engine 4 Tutorial: 180 Degrees Camera Rotation in Top Down Template

In my previous update, I had explained how to implement a 360 degrees camera rotation system in the Top Down Template using the Blueprints. If you're interested, you can check it out here:

Unreal Engine 4 Dev Update #4: Camera Rotation for Top Down Games

Yesterday as part of an experiment, I decided to try a 180 degrees restricted camera rotation system for the same using Blueprints. Well, it turns out that implementing a 360 degrees rotation system is far easier than a 180 degrees system. The additional complexity for doing this with the Timeline nodes comes from the fact that we can't do a loop in this case. As a result the starting position in the Timeline track will be a -90 degrees, while the rotation angle of the camera at level start should be 0 degree. So that makes it necessary to add a few more nodes to make sure that Timeline track starts for the first time at a rotation angle value of zero degree. I'm not sure if this is the most efficient way to do it in Unreal Engine 4. There might even be a better way to do in Matinee or with actual coding. But my method gets the job done in Blueprints, and so far I haven't come across any other methods. If I do, I'll update it in this blog. First I'll post a screenshot of the Blueprint below, as it'll make it a lot easier to understand.

As you can see, I'm using the keys 'Q' and 'E' as the input for rotation till +90 degrees and -90 degrees respectively. I use a Timeline node to make the rotation happen smoothly over time, rather than an abrupt change in the camera angle. First of all, I have created a float variable 'Time1' with an initial value of '-1.0'. I use this to see if I need to start the Timeline at t=0 or at any other specified time. This comes into play only during the first time camera rotation is used. This is because my Timeline starts at t=0 with a value of -90 degrees. At t=1, it reaches 0 degree and proceeds to 90 degrees at t=2. When the level starts, we're actually seeing the equivalent of t=1, i.e. at an angle of zero degree. But when we call the Timeline node for camera rotation, it always starts at t=0, which abruptly throws our camera rotation to -90 degrees, unless we tell it to start at t=1. So basically what I do is that, everytime the necessary input button is pressed, I check if 'Time1'=-1.0, which happens to be it's default value. Once the user has rotated the camera atleast once, I assign a value of 1.0. to 'Time1'. So basically checking if 'Time1'=-1.0 helps to see if this is the first time that the player has used camera rotation. If it is, we hardcode a value of 1.0 into 'Time1'. Then we call a sequence node to first set a new time for the Timeline with the new value of 'Time1'. Then we play the Timeline. If 'Time1' was not equal to -1.0 in the first place, we directly let it play the Timeline using the 'Play' or 'Reverse' input pins based on the player input. The Timeline then gets executed with a length of 2.00 from 0.0 to 2.0. It's a linear graph. Here's a screenshot of it:

We update the Yaw of the SpringArm Component with the key value of the Timeline throughout the 2 seconds. This continuous updating of the Yaw is what helps us to achieve the smooth rotation. Since I've done this in the Character Blueprint, I can directly access the SpringArm component to set it's new rotation. So basically that's how you get a 180 degrees restricted camera rotation using Blueprints in the Top Down template. You can see it in action in the following video:

If you're interested in seeing more of my work in Unreal Engine 4, feel free to check out my Youtube channel as I usually upload the videos there before posting updates here:

So that's it for now. At the moment, I've started working on a Turn-Based gameplay system. Will be putting updates about it soon. Until then, goodbye.