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
Create a new Actor Blueprint
Add a Sphere Collision component to it
Change the Sphere Radius on the collision to a size big enough to surround your VoxelWorld actor
Create an On Component Begin Overlap Event in the Event Graph
Do Character Setup (Below)
Cast from the Other Actor node on the overlap event to your Character Blueprint and pull off the UpdatePlanetGravity function (set up below)
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
Make a Boolean called HasGravity? and connect it to the function start, set it to False
Add a Get Overlapping Actors function and set the class filter to the Sphere Collision Actor class you set up above
Drag a ForEachLoop off the Get Overlapping Actors function and connect it to the execution pin (out) of the HasGravity? (set) boolean
Drag off the ForEachLoop Array Element and cast to the Sphere Collision Actor created above
Drag off the Sphere Collision Actor Cast and add the GetActorLocation function
Add a GetActorLocation function referencing self (The character blueprint)
Subtract (-) the GetActorLocation (self (The character blueprint) ) vector from the GetActorLocation (Sphere Collision Actor) vector
Drag a VectorLength function off the subtraction node result
Create a Local Variable (float) called Distance and drag a get version into the graph
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)
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)
Add a Branch and connect the <= node to the Condition
Connect the Sphere Collision Actor Cast successful pin to the Branch
Add a Local Variable (vector) called Location and drag a set version into the graph
Connect the True pin of the Branch to the Location (set)
Connect the GetActorLocation (Sphere Collision Actor) return value to the Location (set)
Drag a set version of the Distance local variable into the graph and connect it to the execution pin (out) of the Location (set)
Connect the VectorLength return value to the Distance (set)
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)
Drag in the Character Movement component from the components window
Drag a set version of the Gravity Scale float off the Character Movement component and set to 1.0 (for normal gravity level)
Connect Gravity Scale (set) to the execution pin (out) of HasGravity? (set)
Add a Variable (vector) called CentreLocation and drag in a set version
Connect CentreLocation (set) to the Completed pin of the ForEachLoop
Drag in a get version of the Location local variable
Connect the Location (get) to the CentreLocation (set)
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
Drag in a get version of the HasGravity? boolean (created above)
Add a Branch node and connect it to the Event Tick
Connect the HasGravity? (get) to the Condition of the Branch
Add the GetGravityMovementComponent function
Drag off the GetGravityMovementComponent function and add the SetGravityDirection function
Connect the True pin of the Branch to the SetGravityDirection function
Add a GetActorLocation (target = self) function
Add a get version of the CentreLocation variable
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)
Connect the CentreLocation (get) to the GetUnitDirectionVector (to)
Drag off GetGravityMovementComponent and add GetGravityDirection function (Avoid Zero Gravity = False)
Drag of GetGravityDirection return value and add VInterpTo function (GetGravityDirection should be connected to the Current pin of the VInterpTo)
Connect the GetUnitDirectionVector return value to the Target pin of the VInterpTo
Connect Event Tick Delta Seconds to the Delta Time of the VInterpTo function
Set the VInterpTo Interp Speed to 5.0
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).
Add Get Turn function
Add Get Lookup function
Add AddActorLocalRotation function
Connect the Get Turn function return value to the Delta Rotation Z (Yaw) of the AddActorLocalRotation function
Drag multiplication (*) off the Get Lookup function and set the bottom pin to -1.0
Drag Camera Boom (Spring Arm Component) from the components window
Drag off Camera Boom (Spring Arm Component) and add AddRelativeRotation function
Connect the multiplication (*) out pin to the Delta Rotation Y (Pitch) of the AddRelativeRotation function
Connect the AddActorLocalRotation input to the SetGravityDirection output
Connect AddActorLocalRotation output to the AddRelativeRotation input
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:
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