Work In Progress

Designing the Player Controller

Design Architecture document going over the process of creating a Player Controller.


Introduction

The Player Controller is a series of scripts that are designed to allow the player to take on the role of the Player Avatar, otherwise known as the Player Character. Players are able to influence the actions of the Avatar through input via a standard dual-analog controller, including PlayStation 4 and Xbox One controllers. Reactions are then produced by the Avatar based on a combination of that input and a variety of situational variables.


Getting Started

The very basics of the Player to Avatar interaction is dependent on how we expect the character to move. To accomplish movement in Unity we have two (2) main options:

    • Kinematic Controller
      • A movement controller that depends on a series of scripts to determine the outcome of Input and Events, allowing the developer finite control to self-determine exact position and rotation of the Avatar.
    • Physics Controller
      • A movement controller that relies on Unity's built in Physics system to calculate realistic interactions between objects and forces, moving the Avatar accordingly.


The majority of the gameplay in Project Serenade is set in outer space, with the Avatar moving in a friction-less and gravity-free environment. The game Avatar is controlled by a single player, thus some of the pitfalls of using Unity's physics calculations can be avoided. Further, the Avatar will eventually move along the orbits of stars, planets, and other objects.

To this end, I've decided to use a Physics Controller.


Movement Using Physics

In order to create movement inside the Unity physics simulation, we have to apply forces to the object. For our purposes we will need to look at two movement categories:

    • Velocity
      • The speed and direction of an objects positional change, relative to an unchanging point of reference.
    • Angular Velocity
      • The speed and direction of an objects rotational change, relative to an unchanging point of reference.


Combined, these two properties of an object are used to determine its expected position and rotation. This is important because we want the controlling player to be able to move the character in predictable ways based on their input. To accomplish this, we must determine a means of controlling the Velocity and Angular Velocity of the Avatar and apply forces that will interpolate the player input into a predictable change in movement.

So what forces does Unity allow us to apply?

    • Force
      • The basic force that affects an objects Velocity
    • Torque
      • A force that affects an objects Angular Velocity


Directional Player Inputs

Moving forward, the word 'controller' is going to be used very often in describing many things. I'll do my best to provide context where I feel it is needed. For now, think of it as a series of gears working to operate the game, and the 'controllers' are the gears.


What usually comes to mind for the average gamer when they hear the word 'controller' is to think of their favorite console's gamepad. A gamepad is a collection of HIDs, short for Human Interface Devices, with a series of drivers, sensors, and all kinds of other stuff that is handled without our intervention. The important thing is that eventually it all interacts with an Input system in Unity that can be configured to resemble the human facing input names, such as 'Start Button'.

Let's take a look at some gamepad inputs, specifically the ones most related to movement.

    • Left Analog Stick
      • A range of motion two-axis joystick located on the left side of a controller
        • Typically delegated to directly control the Avatar's movement.
      • A click function, recognized as a single digital button.
    • Right Analog Stick
      • A range of motion two-axis joystick located on the right side of the controller
        • Typically delegated to influence the position of the Camera.
      • A click function, recognized as a single digital button.
    • Face Button: Bottom
      • The bottom-most of a series of digital buttons laid out in a cross pattern and located on the right side of the controller
      • Typically the most important digital button on the entire gamepad.

The gamepad is laid out this way because manipulating two analog sticks to control a game is an advanced technique, despite it being second nature to veteran gamers. In most 2D platformer games, like Super Mario Bros., the camera for the game responds directly to player movement, which in turn corresponds to gamepad input. In those cases, both the camera and Avatar are controlled with one joystick.

By locating the two analog sticks on opposing sides, we are able to delegate the movement of the Avatar to the left hand and movement of the Camera to the right hand. What then does this imply about the positioning of the cross patterned face buttons near the Right Analog Sticks?


Setting the Frame of the Camera...

Just like the camera on a phone, the game Camera is attempting to display a 2D representation of a 3D space. With that in mind:

    • Viewing Frame
      • Essentially the border of a picture taken from a Camera.
    • Perspective Point
      • The position and forward facing rotation of a Camera.

Since the Right Analog Stick is what will influence the movement of the Camera, we need to figure out how a two-axis controller can be used to determine a position in 3D space. We accomplish this by having the camera orbit an invisible object who's position is constantly matching the position of the Avatar.

By locking to an orbit, we can commit to controlling the distance of the Camera to the Avatar through another means. A simple implementation for the moment is just to assume that the forward axis of the Camera is a set length away from the Avatar. Imagine that we've tied a string from the Avatar to the Camera, and any position that is at the furthest reach of that string is a possible Perspective Point. Furthermore, the string actually represents the line along which the Camera is facing.

Now, with the forward axis being constant, we can use the two-axis input of the Right Analog Stick to determine the relative up axis and right axis. My desire is to have the Player hold the Right Analog Stick in the direction they would like the camera to look. For instance, if they want to see the rest of a star being visually cutoff by the left side of the Camera frame, they would hold the Right Analog Stick to the left. This causes the camera to move along the orbit in an inversely relative motion to the input, but moves the Viewing Frame so that objects that were positioned to the left of it are now closer to the center.


...and then Moving the Avatar

Now that we have the Viewing Frame set, we can use it to ground a player's sense of control over the movement of their Avatar. This introduces us to the first major skill the players must develop to control movement of the Avatar: an understanding of the game's 3D space as relayed to them through their 2D television.

Directional Orientation

In order to understand what directions mean when viewing a Camera feed, let's break it down a bit in relation to a television's 2D display. Any object in front of the lens of the camera is displayed on the screen, with objects that are further away from the camera being displayed in the background. By defining our directional information as directions based on the center of the real world screen, we can assume the following:

    • Up
      • The space that exists above the center of the screen
    • Down
      • The space that exists below the center of the screen
    • Left
      • The space that exists to the left of the center of the screen
    • Right
      • The space that exists to the right of the center of the screen
    • Forward
      • The space that exists behind the center of the screen.
    • Backward
      • The space that exists in front of the center of the screen.

If you've noticed a disparity in the inverse relationship of the Forward/Backward directions to their real world space counterpart, try to think of this thought exercise. Pick a TV show to watch and focus on some object in the background of the scene. If you imagined trying to reach out and touch it, eventually you'd be moving your hand through the glass of the TV and towards the space behind it.

These directions are what a gamer's brain will intuitively interpret based on the Viewing Frame presented on their screen. If they want to move the character towards objects in the background of the Viewing Frame they'll want to move their character forward. If the object was on the left side of the screen, they'd want to move left. An object above the character, they'd move up. But that's a three-axis set of possibilities...

Frame Relative Movement

At this point we are once again presented with the problem: how to to determine a 3D movement with only a two-axis input. We'll be influencing the Avatar and using input from the Left Analog Stick. If we use the same style solution, we'll need to decide what factor should be determined through a different means.

The long answer is that we'll want it to behave differently depending on the situation. For now, let's concentrate on a specific situation using the most common placement for a Third-Person camera.

    • Position
      • In orbit around the Avatar, directly behind it and slightly up.
    • Rotation
      • Forward Facing direction towards a point that is offset above the Avatar's location.
      • Keeping in mind the Up offset position previously established, facing a point slightly offset from the Avatar's position has several effects:
        • Reverses the lowered horizon effect that raising the camera caused
        • Keeps the Avatar's position in the Viewing Frame closer to the bottom, and thus away from the center
        • Helps to establish two distinct areas of interest in the Viewing Frame
          • better view of what is happening in front of their Avatar.

Let's imagine the effects of each sequentially. By positioning the Camera directly behind the Avatar, both end up facing the same direction. Raising the Camera moves the Avatar and visual horizon's position in the Viewing Frame down.

Moving on to Rotation, the simplest solution would be to have the Camera directly face the Avatar. However, this would cause the position of the Avatar to be located directly in the center of the Viewing Frame, which is undesirable. The center of the Viewing Frame is an important orientation point for the Player to aim with, and generally we want to keep it clear. This is so the Player can move the Camera in a way that puts their desired object of focus in the center of the Viewing Frame without being visually obstructed by the Avatar.

Instead we will pick a point that is slightly offset from the character, which in our current scenario should be slightly above the Avatar. This helps in counteracting the lowered horizon effect introduced when moving the position of the Camera upward, while still maintaining the effect of moving the Avatar towards the bottom of the Viewing Frame.

With the Frame set in this manner, let's take a look at how the Left Analog Sticks input will affect avatar movement.


    • Left Analog Stick
      • Up
        • Towards the frame's forward.
      • Down
        • Towards the frame's backward.
      • Left
        • Towards the frame's left.
      • Right
        • Towards the frame's right.


The concept of movement within a particular frame and under particular circumstances is relatively simple. It is in controlling perspective and relative movement in concert that the concept really starts to expand into more complex territories.

This is important: We have to look at the various circumstances we want to control the avatar in. To the same end, we are going to want to look at circumstances where the player may not have control of camera movement or avatar movement. When the player isn't in control, a computer controlled logic system will need to take it's place.

With so many variables and circumstances and simple A.I. routines, it seems that things would become very complex, very quickly. Yet it is often in these times that we can stumble upon a very important answer. It is unfortunately a long one.


The Long Answer

Let's see if we can't scope out the variables. To do this, we'll take a look a various scenarios and describe what we think should be occurring with the various elements of which it is composed.


    • Avatar Free Movement
      • Movement of an object relative to the camera frame
      • Movement Inputs
        • Left Analog Stick
          • Up
            • Movement towards the frame's forward direction.
          • Down
            • Movement towards the frame's backward direction.
          • Left
            • Movement towards the frame's leftward direction.
          • Right
            • Movement towards the frame's rightward direction.
      • Camera
        • Free Orbit Camera
          • Camera hovers around a look at point that is itself orbiting around another object within an angle restriction.
            • Angle Restriction
              • One arm of the angle is represented by a straight line between the look at point and its orbited object. The second arm of the angle is represented by a straight line from the camera to the look at point.
              • The angle formed by the two arms is restricted by a run time manipulable parameter called the Angle Threshold.
                • Angle Threshold defines the minimum degrees along the look at point orbit that the two arm interior angle can be.
              • Attempted camera movement that would cause the angle to decrease beyond the Angle Threshold causes the look at point to orbit around it's orbited object in the direction of the attempted arm movement.
            • Definable Distances
              • Look At Distance
                • Distance between the look at point and its orbited object.
              • Camera Distance
                • Distance between the camera and its look at point.
              • Angle Threshold
                • See above
          • Camera Inputs
              • Right Analog Stick
                • Up
                  • Rotates the camera around its look at point in the frame's downward direction.
                • Down
                  • Rotates the camera around its look at point in the frame's upward direction.
                • Left
                  • Rotates the camera around its look at point in the frame's rightward direction.
                • Right
                  • Rotates the camera around its look at point in the frame's leftward direction.
              • Triggers
                • Left
                  • Rolls the camera counter-clockwise.
                • Right
                  • Rolls the camera clockwise.
    • Object Orbit Movement
      • Movement of an object around a central point, all possibilities of which are a set distance from the object.
      • Inputs
          • Left Analog Stick
            • Up
              • Circling around the orbited point in the frame's upward direction
            • Down
              • Circling around the orbited point in the frame's downward direction
            • Left
              • Circling around the orbited point in the frame's leftward direction
            • Right
              • Circling around the orbited point in the frame's rightward direction
      • Camera
        • Free Object Half-Orbit Camera
          • Camera orbits within an angle restriction around an object which is itself orbiting around another object. The camera is always rotated to face its orbited object's orbited object.
            • Angle Restriction
              • One arm of the angle is represented by a straight line between the camera and the object the camera is orbiting. The second arm of the angle is represented by a straight line between the camera's orbited object and the object that it is orbiting.
              • The angle formed by the two arms is restricted by a run time manipulable parameter called the Angle Threshold.
                • Angle Threshold defines the minimum degrees along the camera orbit that the two arm interior angle can be.
              • Attempted camera movement that would cause the angle to decrease beyond the Angle Threshold causes no additional movement, maintaining it's position at the closest position in that direction allowed by the Angle Threshold.
            • Definable Distances
              • Camera Distance
                • Distance between the camera and its look at point.
              • Angle Threshold
                • See Above
          • Camera Inputs
              • Right Analog Stick
                • Up
                  • Rotates the camera around its orbited object in the frame's downward direction.
                • Down
                  • Rotates the camera around its orbited object in the frame's upward direction.
                • Left
                  • Rotates the camera around its orbited object in the frame's rightward direction.
                • Right
                  • Rotates the camera around its orbited object in the frame's leftward direction.
              • Triggers
                • Left
                  • Rolls the camera counter-clockwise.
                • Right
                  • Rolls the camera clockwise.
    • Chase Orbit Movement
      • Movement of an object trying to match the position of another object.
      • Input
        • Left Analog Stick
          • Up
            • Move closer to the chased object.
          • Down
            • Move further from the chased object.
          • Left
            • Circle around the chased object in the frame's leftward direction.
          • Right
            • Circle around the chased object in the frame's rightward direction.
      • Camera Mode
        • Free Chase Half-Orbit Camera
          • Camera orbits within an angle restriction around an object which is itself orbiting around another object. The camera is always rotated to face the object being chased.
            • Angle Restriction
              • One arm of the angle is represented by a straight line between the camera and the object the camera is orbiting. The second arm of the angle is represented by a straight line between the camera's orbited object and the object that it is chasing.
              • The angle formed by the two arms is restricted by a run time manipulable parameter called the Angle Threshold.
                • Angle Threshold defines the minimum degrees allowed for the two arm interior angle as the camera moves around its orbit of the chasing object.
              • Attempted camera movement that would cause the angle to decrease beyond the Angle Threshold causes no additional movement, maintaining it's position at the closest position in that direction allowed by the Angle Threshold.
            • Definable Distances
              • Camera Distance
                • Distance between the camera and the chasing object.
              • Angle Threshold
                • See Above
          • Camera Inputs
              • Right Analog Stick
                • Up
                  • Rotates the camera around its orbited object in the frame's downward direction.
                • Down
                  • Rotates the camera around its orbited object in the frame's upward direction.
                • Left
                  • Rotates the camera around its orbited object in the frame's rightward direction.
                • Right
                  • Rotates the camera around its orbited object in the frame's leftward direction.
              • Triggers
                • Left
                  • Rolls the camera counter-clockwise.
                • Right
                  • Rolls the camera clockwise.
    • Rail Orbit Movement
      • Movement around a central point that itself moves along a series of splines that serve as rails.
      • Input
        • Left Analog Stick
          • Up
            • Move central point along the spline in the frame's forward direction.
          • Down
            • Move central point along the spline in the frame's backward direction.
          • Left
            • Circle around the central point in the frame's leftward direction.
          • Right
            • Circle around the central point in the frame's leftward direction.
      • Camera Mode
        • Free Rail Camera
          • Camera that
    • Grounded Movement
      • Movement along the surface of an object.
      • Input
        • Left Analog Stick
          • Up
            • Movement towards the frame's forward direction.
          • Down
            • Movement towards the frame's backward direction.
          • Left
            • Movement towards the frame's leftward direction.
          • Right
            • Movement towards the frame's rightward direction.
      • Camera Mode
        • Grounded Free Look Camera
          • Camera that
    • (Rewrite me) Grounded Target Lock Movement
      • Movement along the surface of an object.
      • Input
        • Left Analog Stick
          • Up
            • Movement towards the frame's forward direction.
          • Down
            • Movement towards the frame's backward direction.
          • Left
            • Movement towards the frame's leftward direction.
          • Right
            • Movement towards the frame's rightward direction.
      • Camera Mode
        • Grounded Target Lock Camera
          • Camera that
    • (Rewrite me) Planar Movement
      • Movement along the surface of an object.
      • Input
        • Left Analog Stick
          • Up
            • Movement towards the frame's forward direction.
          • Down
            • Movement towards the frame's backward direction.
          • Left
            • Movement towards the frame's leftward direction.
          • Right
            • Movement towards the frame's rightward direction.
      • Camera Mode
        • Planar Camera
          • Camera that
    • (Rewrite me) Turret Movement
      • Movement along the surface of an object.
      • Input
        • Left Analog Stick
          • Up
            • Movement towards the frame's forward direction.
          • Down
            • Movement towards the frame's backward direction.
          • Left
            • Movement towards the frame's leftward direction.
          • Right
            • Movement towards the frame's rightward direction.
      • Camera Mode
          • Stationary Tracking Camera
            • Camera position remains stationary while it's rotation changes to face a look at point moving along a plane parallel to the viewing frame and centered on the tracked object.
              • Look At Plane Restrictions
                • The distance that the look at point can move away from the planes center is limited to the plane's edges, which are determined by the plane's scale.
                • Plane Scale
                  • The size, or scale, of the plane is proportional to the size of the tracked object within the frame.
                • Definable Parameters
                  • Vertical Allowance
                    • How far away from center we allow the tracked object to be moved upward or downward in the frame via player input.
                  • Horizontal Allowance
                    • How far away from center we allow the tracked object to be moved leftward or rightward in the frame via player input.
              • Input
                • ddhdhd
    • Standalone Camera Modes
      • Stationary Camera
        • Camera position remains stationary while it's rotation changes to face a look at point orbiting it's position.
        • Inputs
          • Right Analog Stick
            • Up
              • Moves the look at point along its orbit in the frame's upward direction.
            • Down
              • Moves the look at point along its orbit in the frame's downward direction.
            • Left
              • Moves the look at point along its orbit in the frame's leftward direction.
            • Right
              • Moves the look at point along its orbit in the frame's rightward direction.
          • Triggers
            • Left
              • Rolls the camera counter-clockwise.
            • Right
              • Rolls the camera clockwise.
      • Hard Offset Camera
        • Camera that';


##consider target looking - especially grounded ##any special camera modes ##A.I. Viewing Frame ##should look at this without camera relation for A.I. or if applicable ##explain frame movements ##explain avatar movements