How to stop the player capsule from bouncing off corners? (Pictures included.)
How can I stop this from happening? If I could get him to instead bounce straight back down, that would be good. But what I really want is for him to smoothly slide to the side of the block, and keep the same upward momentum, much like what happens in Super Mario Bros. for example. (Yes, in SMB if you jump close to the edge of a block, it won't break, you will slide to the side, like in the image below.)
asked Aug 08 '17 at 04:58 AM in Using UE4
Alright, unfortunately I don't think there's a blueprints answer to this, if that's what you're asking. However I've done some digging around and think I can at least explain how to do it through C++ for posterity.
So to start with, lets get right to the root of the "problem," which is this blasted code in the PhysFalling() function in the UCharacterMovementComponent class in CharacterMovementComponent.cpp:
The arrow points at the line where velocity actually gets set to the new "bounced" velocity. There's also an identical line down at line 4045 that does the same thing in the case of a collision happening after the first collision adjustment on 3995. So, that's our culprit. The most straightforward solution is to comment out lines 3989 and 4045. Of course, we don't want to do that in the engine code. So we have to make our own character movement component that inherits from UCharacterMovementComponent and overrides PhysFalling(), and have our character class use it instead. To do this we have to make sure our character class is using the FObjectInitializer constructor, and then we can follow this guide here.
And that's that. The results of the call to ComputeSlideVector() at line 3983 actually does a halfway decent job of "slipping" the character up and down past inclines and edges, like the result we're looking for. There's a bit of "friction" to it and I don't know if that's some kind of actual material friction or just a peculiarity of the calculations, but its a lot better than bouncing off in crazy ways. I'm sure any clever developer can see their way to more robust solutions and alterations to this, but I think this is a pretty solid starting point. Hopefully this will help down the road.
EDIT: I almost forgot. One negative result of this is that hitting a perfectly horizontal ceiling while traveling upward will have the character just kinda "stick" against it until gravity pulls their velocity back down. This can be circumvented by implementing a check to see if the impact normal is pointing straight down. (which is to say, its Z parameter is rougly -1.0) If it is, just set the Z parameter of Velocity to 0. You can even add more wiggle room to this check, causing the engine to treat slightly angled ceilings like they're flat ones also, if you don't want the character "slipping" up shallow ones.
Follow this question
Once you sign in you will be able to subscribe for any updates here