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
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
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.
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++?
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?
Also, if the planet rotates, the camera starts hopping.
@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.
The result is as follows (Video) it seems to work for the moment, as my project is not in multi.
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.
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:
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:
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?
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 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
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.