Overriding Dynamic Pawn - jgoffeney/Cesium4Unreal GitHub Wiki

Back

This describes some the steps required for created your own Dynamic Pawn.

Cesium Dynamic Pawn

The original Dynamic Pawn is based on the GlobeAwareDefaultPawn class but also has an associated blueprint with additional functionality.

Components

The Dynamic Pawn starts with several components inherited from GlobeAwareDefaultPawn with the parent/child structure as shown below.

  • Collision Component
    • Mesh Component
  • Movement Component
  • Globe Anchor

When created a new version of the Dynamic Pawn a camera will have to be added to the Collision Component as described here.

Blueprints

Event Graph

Set Floating Pawn Ref

When the Pawn BeginPlay() is called it casts the Pawn MovementComponent to FloatingPawnMovement and sets it as the variable FloatingPawnMovementRef.

Scroll to Change Movement Speed

Reads user events for gamepad shoulder buttons, mouse wheel and mouse center buttons to increment or decrement the SpeedMultiplier value.

Toggle Time of Day Widget

Reads T key presses to create a Time Of Day widget and adds it to the viewport if it does not exist. Otherwise it removes it.

Toggle Profiling Widget

Reads P key presses to create a Profiling widget and adds it to the viewport if it does not exist. Otherwise it removes it.

Bind Gamepad Trigger Axes

Bind the gamepad right and lift triggers to the GlobeAwareDefaultPawn Move Up World function to move up and down.

Toggle Dynamic Speed at Runtime

A G key press or gamepad right face button press toggles the value of the variable bEnableDynamicSpeed.

Set Movement Speed

On each Tick it checks if the variable bEnableDynamicSpeed is set and calls the blueprint functions Update Pawn Speed or Set Max Move Speed (which passes in a Speed value of 10000.0).

Update Pawn Speed

  • Gets the last movement vector and if it is near 0 then reset the SpeedMultiplier to 1.
  • Otherwise call SpeedLineTrace to get the ValidSpeedFound, OverrideSpeed and PawnSpeed. Then after some boolean ops set MaxSpeedPreMultiplier on true. Then call SetMaxMoveSpeed with input Speed passed from MaxSpeedPreMultiplier.

Set Max Move Speed

Sets the Pawn Max Speed and Acceleration variables for the FloatingPawnMovementRef. Based on the input speed it reads a value from the Movement Curve and multiplies it by the SpeedMultiplier to set Max Speed. The Max Speed is then multiplied by 5 and clamped to set the Acceleration.

Speed Line Trace

This function is called with the camera location vector and camera rotation rotator as inputs that feed directly into a Sequence node with three outputs that calls each of the following code blocks.

Sequence 0

Casts a trace from the camera location to the center of the earth, finds the first collision and computes the height as the difference from the camera to the collision (but only if there was a collision).

Sequence 1

Casts a trace from the camera location to its next potential location to see if it is going to collide with something in the next step. The CameraTraceDistance local variable is set based on the height difference between the next collision location (but only if there was a collision).

Sequence 2

Sequence 2 contains several steps based on the previously computed values.

  • If the Height is almost 0 then return the Pawn Speed as 0.
  • If Height divided by MaxSpeedPreMultiplier is greater or equal to 100 then return the Pawn Speed as 0.
  • If CameraTraceDistance is almost 0 (no future collision) then:
    • Return ValidSpeedFound as true
    • Return OverrideSpeed as true if Height is greater than equal to DynamicSpeedMinHeight.
    • Return PawnSpeed as Height
  • If the Height and CameraTraceDistance are greater than 0 (each hit):
    • Return ValidSpeedFound as true
    • Return OverrideSpeed as true
    • Return PawnSpeed as Height

New Dynamic Pawn

Create a new C++ class based on GlobeAwareDefaultPawn. The following functions are the overrides of the Unreal DefaultPawn class where you will probably want to update to make it do something different.

Note: you will likely want to call the Super version of the functions. For example, if you do not call GlobeAwareDefaultPawn::Tick then the pawn will not fly between points.

  • Motion Functions
    • MoveForward
      • Handles back and forth motion and transforms it with regard to the ellipsoid.
    • MoveRight
      • Handles left and right motion and transforms it with regard to the ellipsoid.
    • MoveUp_World
      • Handles up and down motion and transforms it with regard to the ellipsoid.
    • GetViewRotation
      • Gets the camera rotation
    • GetBaseAimRotation
      • Gets the same value as GetBaseAimRotation
  • General Functions
    • ShouldTickIfViewportsOnly
    • Tick
    • PostLoad

Blueprints

Components

  • Add a CameraComponent as a child to the CollisionComponent.
    • Set Camera Options->Use Pawn Control to True.
      • If this is not enabled then you will not be able to use the mouse to move around.
    • Note: the CollisionComponent is defined as private in the DefaultPawn class. Therefore it is not possible to add the Camera directly within code.
  • Open the Movement Component
    • Floating Pawn Movement
      • These values keep the pawn from floating around / bouncing off terrain.
      • Max Speed
        • Set to 100000
      • Acceleration
        • Set to 2000000
      • Deceleration
        • Set to 999999999999 (or something big)

Variables

Add the following variables:

  • MaxSpeedPreMultiplier
    • Type: Float
    • Default Value: 0.0
  • SpeedMultiplier
    • Type: Float
    • Default Value: 1.0
  • Increment
    • Type: Float
    • Default Value: 1.5
  • Decrement
    • Type: Float
    • Default Value: 1.5
  • MovementCurve
    • Type: Curve Float
    • Default Value: Curve_PawnMovement_Float
  • bEnableDynamicSpeed
    • Type: Boolean
    • Default Value: True
    • Visibility: Public
  • DynamicSpeedMinHeight
    • Type: Float
    • Default Value: 2000.0
    • Visibility: Public
  • FloatingPawnMovementRef
    • Type: FloatingPawnMovement
  • TimeOfDayRef
    • Type: WBP Time of Day
  • ProfilingRef
    • Type: WBP Profiling

Functions

Note: when adding function Inputs if you have an instance of DynamicPawn loaded you may need to use a different parameter name. For example in SpeedLineTrace when trying to add CameraLocation and CameraRotation parameters Unreal would not allow it so I used CameraLoc and CameraRot.

Add the following functions:

SpeedLineTrace

  • Inputs:
    • CameraLocation
      • Type: Vector
    • CameraRotation
      • Type: Rotator
  • Local Variables
    • ActorLocation
      • Type: Vector
    • Height
      • Type: Float
    • CameraTraceDistance
      • Type: Float

Copy the function body from DynamicPawn to your function. Connect the Speed Line Trace node's exec pin to the Sequence node.

All of the previous references to CameraLocation and CameraRotation inputs have to be updated as they are now broken.

SetMaxMoveSpeed

  • Inputs
    • Speed
      • Type: Float

Copy the function body from DynamicPawn to your function. Connect the Set Max Move Speed node's exec pin to the Is Valid node and the Speed input to Get Float Value parameter In Time.

UpdatePawnSpeed

Copy the function body from DynamicPawn to your function. Connect the Update Pawn Speed node's exec pin to the Branch node.

Fix the input parameters from the BreakMinimalViewInfo node to the Speed Line Trace node. The Location and Rotation outputs need to be hooked up to the Speed Line Trace input parameters.

Event Graph

Copy the event graph from Dynamic Pawn to your blueprint. For the error on Toggle Dynamic Speed pick the option Do nothing. Within the blueprint select the ToggleDynamicSpeed event and select Refresh Node. At this point everything should compile.

Update Details

Update values in the blueprint Details tab.

  • Cesium
    • Fly to Altitude Profile
      • Set to Curve_AltitudeProfile_Float
    • Fly to Progress Curve
      • Set to Curve_Progress_Float
    • Fly to Maximum Altitude
      • Set to Curve_MaxAltitude_Float
  • Pawn
    • Auto Possess Player
      • Set to Player 0

World Component

Drag the blueprint onto your level.