Tutorial: Custom Gravity in UE 5.4

Unreal 5.4 introduced support for custom gravity in the CharacterMovementController. See how to take advantage of it and do cool things like gravity puzzles or create a mini-planet like gravity game.

https://dev.epicgames.com/community/learning/tutorials/w6l7/unreal-engine-custom-gravity-in-ue-5-4

7 Likes

Thanks a lot for this, I was at the moment looking to define gravity with my planet plugin.
Do you think it’s working with any types of pawns but not just the character one (in case I’m working on a RTS for example)?

Thanks again, hope for more :slight_smile:

The built-in custom gravity in UE is in the CharacterMovementComponent, which I think only the Character class contains.
If you are using pawns without that component you probably have a simpler way of moving them, in which case you can just uncheck the “use gravity” checkmark and apply a physics force to them each tick to get the same effect.

1 Like

What do you have to change to get this to work for the first-person character?

In the tiny planet example, your camera follows all axis properly. Is there a blue print way to take advantage of just that option?

I have written a flight controller for a space suit and am hoping to add 6 degrees of freedom with camera in tow, but not in control.

@Estrange I haven’t tried it but the camera is probably relative to the character so you might not need the GravityController class. But I’m not sure, test it!

@ShawnDaGeek: If you want to do the input handling through the controller then I couldn’t find a BP-only way to do that.
But you might be able to get around it by removing the Character’s Camera Boom (SpringArmComponent) and handling the camera rotating yourself by moving the camera directly, then you can keep its rotation relative to the parent Character Blueprint. Worth a shot maybe if you don’t want to use C++?

1 Like

Hey Thanks a lot for your tutorial. I’ve been looking for a simple solution to this problem on Unreal Engine for a long time.
Unfortunately, I ran into a problem while following your tutorial for creating moving planets? When the character jumps, his jump does not follow the movement of the planet. Could you please give me an idea of how to solve this problem? :smiley:

Gravity Test Video

Also, if the planet rotates, the camera starts hopping.

Gravity Camera Problem

@StellarDrop now we’re getting into the difficulties of physics in games indeed!

When the planet is moving then you’re moving the player character around also, and the jumping probably doesn’t take that into account. The CharacterMovementComponent has lots of assumptions about gameplay which might not work with circular gravity like this.

There are different ways games get around limitations like that. One way is to stop the planet the player is on but rotate the rest of the solar system around it instead, that way you get the same effect but don’t have to worry about player physics.

Thank you very much for your reply.
Yes, it seems rather complex and beyond my skills at the moment

However, I’ve managed to get around the problem in the meantime with a very basic method. I attach the character to the planet with an “Attach Actor to actor” node only when jumping, and I attach the “Spring Arm” and the camera to a “Scene Component” that is detached from the player at Begin Play and then follows the character’s movement and rotation at each Tick.
I also deactivate the “Impart” parameters so that the planet’s speed doesn’t influence the jump height.


Capture d'Ă©cran 2024-04-05 173803

The result is as follows (Video) it seems to work for the moment, as my project is not in multi.

Planetary Gravity Test

1 Like

Hey @Ari_Epic , thanks for this simple solution, i’ve trying to achieve this result for a long time and by diferents mean. I’m having just one problem right, i’ve created a actor for the PLANET and i added a sphere to it. in the BP_ThirdPersonCharacter i put it the tick funciton as you mentioned with the Target Gravity Actor variable being my PLANET actor, but the validator get is getting ‘is not valid’ for some reason.


image
i notice that i can’t select the default value, it’s says ‘Editing this value in a Class Default Object is not allowed’, i have tried to make the variable as a ref to ACTOR, but still i’m not able to select a default value. i know i’m doing something wrong, i just don’t know where is the problem, i would be glad if someone help me. thanks from brazil!

Hey @Ari_Epic, this is a great tutorial. I was able to get it implemented pretty quick. I am having an issue that I am wondering if its something that is by design and its something that I have to replicate myself or if its still being developed. I have the issue of when you have the Set Gravity Direction replicated the capsule component along with all the other children do not replicate. On each players screen it looks fine to the player controlling just the other players are misaligned from the new gravity direction. Any input from anyone would be greatly appreciated.
Thanks in advance.

hey, i don’t know about you problem, but i have asked above about a problem i’m having. how you set up your planet? was a actor blueprint with a mesh? and in the TargetGravityActor, you put a reference for what? i would be glad if you could help me with that

@Tuafo Yeah you need to get a reference to the planet actor at runtime.

There are many ways to go around it, an easy one would be to just place your character in the level, set the Planet reference there, and make sure to have the player auto-posses the character:


image

Then you would not be using the Player Start, you could delete that.

Another way would be to get a reference to the Planet actor in the Character’s BeginPlay function. Like this (but add checks and stuff):

Third way would be to make the variable a soft object reference, then you can set it in the Class Defaults using the eye-drop button:



image

Yeah, thanks so much for your help, i’m really happy, I have been trying to achieve this result for a long time. thank you so much for your help!!!

hey, i was testing, the solution you passed work it, but i was testing with 2 players (X e Y) and when for example, the character X walks around, the character Y sees him getting into the falling animation, but really fast, like a glitch. It’s was suposed to be like that? or it is someting that i need to set up about the replication?

1 Like

Nice to see you got it working! I noticed a similar issue with rotating bodys causing the camera to hop all over. Did this also solve that issue or just the jumping one?

HI :slight_smile: thanks for the tutorial, i did one myself before finding this post and it has been a great help and I have been very much looking forward to this feature for a while

Could I perchance get you cast your eyes on my current forum post, i am trying to replicate the custom gravity but I am having orientation issues and I don’t know if there is a bug with it or I am doing something completely wrong :smiley:

Thanks
Saragan

@Ari_Epic
I managed to get a sub-classed LyraController setup for this,
Any suggestions on best practices to sub-class & override the LyraCharacter LyraHeroComponent functions needed in the “Gravity Relative Mouse Input” changes, since the input for that project is handled in a C++ Component instead of directly in the Chars BP?

EDIT: Workaround for now that I found is to make a custom LyraHeroComponent in the project, Converting the Input_Move function from virtual void to a UFUNCTION(BlueprintImplementableEvent) void & then setting up the needed refs on begin play, gating the move input behind a validated get.

Finally figured it out. If you place the character with controlled gravity on any rotating body. Make sure to enable “Ignore Base Rotation” in the character movement component. I was continuously setting the characters gravity towards the center of a planet and whenever the character made contact with it while it was rotating the camera would freak out since it was inheriting the rotation from the character.

4 Likes