Free project and plugin download link on the bottom
In the following article you will find out how to override gravity from default Unreal Engine 4 Character. After this change it will be possible any time in the game define gravity vector so character uses it instead of original behavior.
Default Unreal Engine 4 gravity has no directional vector. Gravity points always towards ground FVector = (0,0,-1). Component responsible for updating character in World is CharacterMovementComponent which is attached to the Character. To change default class object Super call from constructor can be used:
: Super(ObjectInitializer.SetDefaultSubobjectClass(
ACharacter::CharacterMovementComponentName))
{
}
To custom your own directional gravity necessary is to override bunch of virtual UCharacterMovementComponent methods:
Public methods:
|
|
Protected methods:
|
|
As you can see there are quite few methods which needs to be implemented according to implement directional gravity which works well with newly custom gravity vector. I will do walk through all methods in the second part but for now I just explain how plugin works so you can download it and try on your own!
Starting point for analyse is StartNewPhysics() because our movement is physic based. It always needs to update it before performing movement. You can check that every frame it is updated with new PhysMode. Depends on circumstances character may walk, fly, swim, fall etc.
Available movement phys modes:
- MOVE_Walking – PhysWalking
- MOVE_NavWalking – PhysNavWalking
- MOVE_Falling – PhysFalling
- MOVE_Flying – PhysFlying
- MOVE_Swimming – PhysSwimming
- MOVE_Custom – PhysCustom
which are resolved by one on function with similar name. So for example starting new physics with mode MOVE_Walking is resolved by PhysWalking function.
It calculates new velocity and performs walking movement.
Simple blueprint methods:
To make life easier some setting methods were expose to blueprints like
- GetGravityDirection()
- SetGravityDirection()
which can be used to compute and update gravity direction for characters.
Free project and plugin:
In the free project below you can find different examples showing how gravity works and how affects not only our Pawn but also StaticGravityCharacter (Cows, boxes).
PlanetsExample
The map shows how planet gravity works.
SimpleGravityExample
The map shows simple gravity vector changes, rapid navigation changes.
Download:
Simple and practival even if it requires overriding several methods from CharacterMovementComponent.
Hi man would you consider doing a video tutorial for someone like me ? I’d like to know the nitty gritty of how it works ?
Hey Ryan, sure I am considering doing some C++ tutorials and explaining how to override gravity or how plugin works is very good idea.
Hello! Thanks a lot for sharing this. Tried this shortly, runs fine. Obvious question here (and, I apologize if there is already obvious answer) – how could I get flying pawn to be gravity bound (around planet going), with that bounding disappearing at some distance from particular “planetoid”? Thank You in advance!
There is example how it is made. Currently implementation is pretty simple for spotting new planets – round collision around the planet so when you escape -> OnActorEndOverlap it just stops updates gravity based on planet and when reaching new planet OnActorBeginOverlap it updates new planet – center of the planet is taken for calculations directional gravity vector. You may want to check *PlanetsExample*.
Hello! Thank you for sharing this very useful plugin! I do like to use this; meantime, I am studying ue4, so I really want to know how to make this plugin. I want to make a game that everything using my own cord. I wonder, whether your code is shown publicly? I am looking forward to your tutorial video. Thank you very much.
You can get it from maketplace and check engine plugins content https://www.unrealengine.com/marketplace/en-US/product/directional-planet-gravity
It’s also available on GitHub you can go directly http://github.com/Tefel/DirectionalGravity