Understanding the couple Pawn (or Character) and Controller

Using an AController in combination with a Pawn provides a more robust and flexible system for controlling game characters and NPCs, both from a player and AI perspective.

In Unreal Engine, a Pawn represents a player or non-player character that can be controlled in the game world. The AController is responsible for managing the Pawn’s input, movement, and behavior.

It receives input events from the player, such as keyboard and mouse input, and translates those into movement commands for the Pawn.

Using an AController in combination with a Pawn in Unreal Engine 4 provides several benefits. One of the primary advantages is the separation of the command (Input) and the action.

Imagine that in your Pawn tick function, you check if a keyboard input has been pressed and move the pawn accordingly. In that case, your pawn can only be controlled by a player. However, a better approach is to have a Move() function in your Pawn, and the input check in a APlayerController. With this structure, you can decide to replace the APlayerController with a AAIController and thus transform your player character into an NPC.

In Unreal Engine, we say that a Pawn is possessed by a Controller (whether it’s a APlayerController, a Paracontrolled, or a derived class). When a Pawn is possessed by a Controller, the Controller can send commands to the Pawn and move it around the game world.

APlayerController example

Here’s an example of how to set up a PlayerCharacter, a component, and a Controller in Unreal Engine:

In AMyCharacter.h:

UCLASS()
class AMyCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    // Move the character in the given direction
    UFUNCTION(BlueprintCallable, Category = "Movement")
    void Move(FVector Direction);
};

In AMyCharacter.cpp:

void AMyCharacter::Move(FVector Direction)
{
    AddMovementInput(Direction);
}

In the APlayerController class, you can call the Move() function on the possessed character by getting a reference to it and passing the movement direction:

void AMyPlayerController::MoveForward(float Value)
{
    if (Value != 0.0f)
    {
        // Get the possessed character
        AMyCharacter* MyCharacter = Cast<AMyCharacter>(GetPawn());

        if (MyCharacter != nullptr)
        {
            // Calculate the movement direction
            FVector Direction = GetControlRotation().Vector() * Value;

            // Call the Move function on the character
            MyCharacter->Move(Direction);
        }
    }
}

This example assumes that you have set up the input bindings for MoveForward in the Input section of the Project Settings. It also assumes that the AMyPlayerController class is a derived class of APlayerController.

APlayerController has some useful function such as

  1. SetViewTargetWithBlend: Changes the view target of the player controller. This is used to switch between different cameras or control different pawns.
  2. AddYawInput and AddPitchInput: Adds input to the yaw (left/right) or pitch (up/down) axis of the controller. This is used to control the camera or character rotation.
  3. GetMousePosition: Returns the current position of the mouse cursor on the screen.
  4. ClientPlayCameraShake: Plays a camera shake on the client side, which can be used for things like screen effects or impact feedback.
  5. GetHUD: Returns the HUD object that is associated with the player controller.

Also you have to note that a controller is associated with the camera through the use of a player camera manager. The player camera manager is a special actor that manages the view and position of the camera for a specific player controller. When a player controller is created, it automatically creates an associated player camera manager.

AAIController

Additionally, the AAIController allows for more advanced AI behaviors. For example, an AI-controlled enemy can use its AController to navigate around obstacles and engage in combat with the player character.

Some important function includes:

  1. MoveToLocation: Orders the pawn to move to a specific location.
  2. MoveToActor: Orders the pawn to move to a specific actor.
  3. SetFocus: Sets a focus actor for the AI (Orient the Pawn to the direction of the focused actor).
  4. SetGenericTeamId(): Sets the team ID for the AI controller.
  5. GetAIPerceptionComponent: Gets the AI perception component responsible to handle stimuli: perception of an enemy/friend, hearing a noise (e.g. in steahth games),  etc…

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *