Directional & Planet Gravity

Directional & Planet Gravity is a plugin available for free on the Unreal Marketplace: https://www.unrealengine.com/marketplace/en-US/slug/directional-planet-gravity

Setup with Voxel Plugin in Blueprints

Sphere Collision Setup

  1. Create a new Actor Blueprint

  2. Add a Sphere Collision component to it

  3. Change the Sphere Radius on the collision to a size big enough to surround your VoxelWorld actor

  4. Create an On Component Begin Overlap Event in the Event Graph

  5. Do Character Setup (Below)

  6. Cast from the Other Actor node on the overlap event to your Character Blueprint and pull off the UpdatePlanetGravity function (set up below)

  7. Place the Sphere Collision Actor in your level and copy the Location coordinates of your VoxelWorld actor to the Sphere Collision Actor location coordinates so that it sits at the centre of your VoxelWorld sphere

Character Setup

Inherit from Gravity Character

The character blueprint for your project must inherit from Gravity Character, which is available once you have installed the Directional & Planet Gravity plugin. Inheriting from the Gravity Character changes the Character Movement component so it responds to the new direction of gravity. This means that you can't inherit from the VoxelCharacter, so your character blueprint will need a VoxelInvoker component (from Voxel Plugin) to work with the VoxelWorld actor.

Create UpdatePlanetGravity Function

Create a new function on your character blueprint called 'UpdatePlanetGravity' with the following blueprint code

  1. Make a Boolean called HasGravity? and connect it to the function start, set it to False

  2. Add a Get Overlapping Actors function and set the class filter to the Sphere Collision Actor class you set up above

  3. Drag a ForEachLoop off the Get Overlapping Actors function and connect it to the execution pin (out) of the HasGravity? (set) boolean

  4. Drag off the ForEachLoop Array Element and cast to the Sphere Collision Actor created above

  5. Drag off the Sphere Collision Actor Cast and add the GetActorLocation function

  6. Add a GetActorLocation function referencing self (The character blueprint)

  7. Subtract (-) the GetActorLocation (self (The character blueprint) ) vector from the GetActorLocation (Sphere Collision Actor) vector

  8. Drag a VectorLength function off the subtraction node result

  9. Create a Local Variable (float) called Distance and drag a get version into the graph

  10. Set the value of the Distance local variable to either the distance from the centre that you want gravity to work or a very high number for any distance (e.g. 99999999999999)

  11. Drag a less than or equal to node (<=) off the VectorLength and connect the Distance (get) variable to the bottom pin (checks if the VectorLength <= Distance)

  12. Add a Branch and connect the <= node to the Condition

  13. Connect the Sphere Collision Actor Cast successful pin to the Branch

  14. Add a Local Variable (vector) called Location and drag a set version into the graph

  15. Connect the True pin of the Branch to the Location (set)

  16. Connect the GetActorLocation (Sphere Collision Actor) return value to the Location (set)

  17. Drag a set version of the Distance local variable into the graph and connect it to the execution pin (out) of the Location (set)

  18. Connect the VectorLength return value to the Distance (set)

  19. Drag a set version of the HasGravity? boolean into the graph, set it to True, connect it to the execution pin (out) of Distance (set)

  20. Drag in the Character Movement component from the components window

  21. Drag a set version of the Gravity Scale float off the Character Movement component and set to 1.0 (for normal gravity level)

  22. Connect Gravity Scale (set) to the execution pin (out) of HasGravity? (set)

  23. Add a Variable (vector) called CentreLocation and drag in a set version

  24. Connect CentreLocation (set) to the Completed pin of the ForEachLoop

  25. Drag in a get version of the Location local variable

  26. Connect the Location (get) to the CentreLocation (set)

alt=UpdatePlanetGravity Function on Character Blueprint|thumb|UpdatePlanetGravity Function on Character Blueprint

Update Gravity on Event Tick

To update the gravity direction as the character moves around the sphere, add the following to the Event Tick in your Character Blueprint Event Graph

  1. Drag in a get version of the HasGravity? boolean (created above)

  2. Add a Branch node and connect it to the Event Tick

  3. Connect the HasGravity? (get) to the Condition of the Branch

  4. Add the GetGravityMovementComponent function

  5. Drag off the GetGravityMovementComponent function and add the SetGravityDirection function

  6. Connect the True pin of the Branch to the SetGravityDirection function

  7. Add a GetActorLocation (target = self) function

  8. Add a get version of the CentreLocation variable

  9. Drag off the return value of the GetActorLocation function and add a GetUnitDirectionVector function (GetActorLocation should be connected to the from pin of the GetUnitDirectionVector)

  10. Connect the CentreLocation (get) to the GetUnitDirectionVector (to)

  11. Drag off GetGravityMovementComponent and add GetGravityDirection function (Avoid Zero Gravity = False)

  12. Drag of GetGravityDirection return value and add VInterpTo function (GetGravityDirection should be connected to the Current pin of the VInterpTo)

  13. Connect the GetUnitDirectionVector return value to the Target pin of the VInterpTo

  14. Connect Event Tick Delta Seconds to the Delta Time of the VInterpTo function

  15. Set the VInterpTo Interp Speed to 5.0

  16. Connect the VInterpTo return value to the SetGravityDirection (New Gravity Direction) pin

Update Camera Boom and Actor Rotations for Third Person Character on Event Tick

This turns your camera boom and actor so the character appears to stay upright when running around the sphere.

These nodes are also added to the Event Tick after the Update Gravity code (above).

  1. Add Get Turn function

  2. Add Get Lookup function

  3. Add AddActorLocalRotation function

  4. Connect the Get Turn function return value to the Delta Rotation Z (Yaw) of the AddActorLocalRotation function

  5. Drag multiplication (*) off the Get Lookup function and set the bottom pin to -1.0

  6. Drag Camera Boom (Spring Arm Component) from the components window

  7. Drag off Camera Boom (Spring Arm Component) and add AddRelativeRotation function

  8. Connect the multiplication (*) out pin to the Delta Rotation Y (Pitch) of the AddRelativeRotation function

  9. Connect the AddActorLocalRotation input to the SetGravityDirection output

  10. Connect AddActorLocalRotation output to the AddRelativeRotation input

alt=Event Tick Update of Gravity, Camera and Actor|thumb|Event Tick Update of Gravity, Camera and Actor

Known issues

Turn Direction Swapping

When the character reaches the lower parts of the sphere the turn direction swaps (right = left/left =right) - working on a solution and will post when known

Orientation Snapping

When using this plugin on a large spherical world, your orientation will occasionally snap into place as your desired rotation and current rotation exceed a specified threshold.

This is located at line 2606 in the gravitymovementcomponent.cpp which states the following:

// Abort if angle between new and old capsule 'up' axis almost equals to 0 degrees.
    if ((DesiredCapsuleUp | GetCapsuleAxisZ()) >= THRESH_NORMALS_ARE_PARALLEL)
    {
        return;
    }

This can be commented out, or the threshold can be changed to suit your needs. If it is commented out, it will check and set your orientation every frame.

Last updated