This tutorial will show you how to change the default unreal camera into a fixed scrolling camera for a single player game. It can be changed for side-scrolling, top down, or any angle inbetween. I will assume that you know the basics of unrealscript, such as how to compile your code, and how variables work. If you’re just beginning, check out UDN, Unreal Wiki, and the 3d Buzz video tutorials. In learning Unrealscript, the beginning can be the most frustrating, as most of the time ends up being spent researching how things are working and digging through higher-up classes and scattered documentation in order to figure out what is doing what.

Changing the camera behavior is pretty simple, and you can get it working with a few lines. If you are using the series of Unreal Runtime examples found here (http://udn.epicgames.com/Two/MyFirstCode#My_First_Code), simply add the following code to your ExampleController.uc. Note that this is made with a singleplayer game in mind where the camera will only be viewing from one player. If you are not working with the Runtime, you will need to create a custom gameinfo that assigns this new MyController to PlayerControllerClassName. The code:

Working Code

//=======================================
class MyController extends PlayerController
//=======================================

event PlayerCalcView( out actor ViewActor,
                      out vector cameraLocation,
                      out rotator cameraRotation )
{
ViewActor = self;
bBehindView = True;
cameraLocation = Pawn.Location;

cameraLocation.X += CollisionRadius + 0;
cameraLocation.Y += CollisionRadius + 15;
cameraLocation.Z += CollisionHeight + 250;

cameraRotation.pitch = 0;
cameraRotation.yaw = -16384;
cameraRotation.roll = 0;
}

Explanation

After the class declaration, we are overriding the event PlayerCalcView. It’s easiest to think about the keyword ‘event’ as meaning the same as ‘function’ *(see subtext 1 at bottom for detailed explanation). PlayerCalcView already exists in the playerController class. You can take a look at playerController and other scripts in the EngineClasses directory of Unreal. By overriding this function/event, we are creating a new version of the function to be used in this subclass.

The function playerCalcView takes 3 arguments, but also returns 3 values using the out keyword. The out keyword allows functions to have more than one return value. When the function returns, it copies the new values back to the original variables passed as arguments.

ViewActor = self;
bBehindView = True;

The returned actor ViewActor should be assigned so any other classes calling the function will have the correct reference. We assign it to “this actor”, or the object whose script is being executed, using the name=”code” command ‘self’. bBehindView is a boolean that already exists in playerController. It is needed to put the camera in third person mode. The letter b beginning the variable name is typically used to represent variables which are booleans.

cameraLocation = Pawn.Location;

cameraLocation.X += CollisionRadius + 0;
cameraLocation.Y += CollisionRadius + 250;
cameraLocation.Z += CollisionHeight + 15;

Now we need a reference to the player’s pawn, so we can move the camera along with the player. Pawn’s are players and AI’s that move around in the game. Since we are inside of the custom player controller, we can access its pawn, directly.

Controllers control pawns. And “Player controllers (any class that extends PlayerController.uc) are the controllers that serve as the interface from a human player to a pawn. The player controller to use is specified in the GameInfo.” -UDN

Note: If you need a reference to the player controller outside of its class, you can use Level.GetLocalPlayerController(). Remember it may need to be typecasted to access new member functions and variables added to the custom controller (in this example being called myController).

The player pawn’s location is assigned to the cameraLocation. So the camera is at the same location as the player, which isn’t going to be of much good. We need to move it away and rotate it so we can see the player. CameraLocation (and pawn.location) are stored as vector structures, which have X,Y, and Z components. The X axis is front to back, the Y left to right, and the Z up and down. They are accessed like classes and subclasses, using a period ( . ).

By adding or subtracting from the X,Y, and Z, we move the camera away from the player location in Unreal Units. Adding the collision radius and height is not necessary but ensures that the camera always moves the same distance away from the outside of the player’s collision cylinder; the player model has a collision cylinder around his/her/itself defined by collisionHeight and collisionRadius. I used the values above and below in a sidescroller. Mess around with them to get the camera where you want it.

cameraRotation.pitch = 0;
cameraRotation.yaw = -16384;
cameraRotation.roll = 0;

The cameraRotation is a rotator, which is a struct similar to vector, which has pitch, yaw, and roll components. Yaw is shaking your head left and right, pitch is nodding up and down, and roll is side to side.The numbers being assigned here determine how much the camera is rotated, not using degrees or radians, but unreal’s own rotator units. For rotations in unreal, the following numbers are used:

  • 65536 = 360 degrees
  • 32768 = 180 degrees
  • 16384 = 90 degrees

You may want to adjust the pitch to point the camera slightly down or up at a character in a sidescroller. For a top-down camera, simply adjust the pitch by 90 degrees, and increase the Z location offset.

So that’s it. I hope this was helpful.

I notice that a lot of people are still viewing this article; does this still apply to Unreal 3?


*subtext 1
“The event keyword has the same meaning to UnrealScript as function”. However, when you export a C++ header file from Unreal using =unreal -make -h, UnrealEd automatically generates a C++ -> UnrealScript calling stub for each “event”. This automatically keeps C++ code synched up with UnrealScript functions and eliminates the possibility of passing invalid parameters to an UnrealScript function. For example, this bit of UnrealScript code:” – Unreal Script Reference

Comments on this Article

  1. Shahzaib says:

    hello sir!
    this very helpful but i want to know how could i disable mouse movement and use the same camera for vehicle as well as pawn…?

    • Josh Winn says:

      I haven’t worked with Unreal in a while; this article is from 2006. I’d ask around on some of the dedicated UDK scripting help forums (Epic Games or 3d Buzz forums probably). There’s also http://gamedev.stackexchange.com/.

Leave a Reply

You can use the <pre> tag to post a block of code, or <code> to highlight code within text.