Programming jumping and gravity?

So I am currently trying to add gravity and jumping to my sidescroller character.
I’ve followed the the components tutorial in the official documentation, so I am using movement components successfully and as it states, indeed the pawn does not yet have gravity implemented… But how would one go about adding that exactly? I assume movement components already got this covered. So how do I make use of it?

Likewise, I’ve found the Jump and Dojump() functionality, but when I bind a jump key, nothing happens when the button is pressed, yes it is defined in both my pawn and editor. Any suggestions? What else do I need to use the jumping in movement components?

Most responses I’ve found pretty much just state “use the jump function”, or “add a movement component, it has physics”, which doesn’t really help.

Hey -

Could you explain what you have setup so far and link the tutorial you were following? Did you start with a new class based on character or pawn? As for binding an input button to your jump, you would want to do this in the character’s SetupPlayerInputComponent() function. This allows you to set what is bound (what is inside the quotes needs to match what the editor says for the input button) and what function to call when the input is used. Let me know if this helps or if you have further questions

Cheers

I’ve followed Components and Collision | Unreal Engine Documentation roughly (adapted it to the game I’m trying to teach myself to make), and I’ve extended my player character from the “character” class.

I have indeed bound my Jump action in the SetupPlayerInpitComponent() function, I have setup other keys that are working fine.

I just noticed that the components tutorial uses pawn instead of character, while Player Input and Pawns | Unreal Engine Documentation uses character (which I followed originally)

I’ve tried parenting both pawn and character, but the jump action does nothing on either and neither have gravity.

Character derives from Pawn so it should have all of the functionality that Pawn has. I will follow up again after working through the tutorial myself so I have a better understanding of what’s happening.

I see, please let me know once you’ve had a look.

Hey -

For setting up gravity you should be able to call SetSimulatePhysics for the components you want to react to gravity. Using the Root Component from the tutorial as an exampled (called SphereComponent) this would like like such: SphereComponent->SetSimulatePhysics(true).

To enable jumping you should be able to look at the code for the third person character template project. Inside the SetupPlayerInputComponent() function, Jump is bound to call the base ACharacter’s Jump function. If your character class inherits from Character you should be able to use the same code in your class to easily enable jumping.

Cheers

Adding SetSimulatePhysics(true) was certainly interesting, and will be very useful in the future, but I am just looking make my character able to fall, I don’t want to turn it into a ragdoll.

I will have a look at the templates to get a bit more of an understanding of existing functions. My game is based on the sidescroller template already, which does indeed have a jump function, however it seems to be done entirely through blueprint instead of c++ ?
As I stated in the first post and by you, the parent classes already have jumping functionality. Shouldn’t it be working just by calling the jump() function?

As far as I can tell from other people working with jumping, it should be that simple, but perhaps something in my code is forcing it not to work?

Here’s my character.cpp so far: http://pastebin.com/rhy2vDvH

Hey -

The code in your source file seems correct. If you are having problems getting the character to jump so far, can you provide a copy of the project for me to test directly? If you’re able to upload the project to dropbox you can then post a download link here or send me a private message on the forums for privacy.

Certainly, sorry for the terribly late reply.

I have sent a link to the project to your forum account.

Hey -

I received the download link however the structure that it is in makes it difficult to download. Could you remove the Saved and Intermediate folders to make the project smaller and then zip the entire project folder and upload the .zip file?

Alright, you can now find a .rar file at the link location.

Hey -

I noticed that your character class has a custom CharacterMovementComponent attached to it as well as a custom skeletal mesh component. If you are inheriting from Character, these components are already included as part of the class so you should be able to use the default components rather than needing to add your own. I believe this may have something to do with why your jump isn’t working properly, it’s likely that the movement component being invoked when the call to ACharacter::Jump (as set in your SetupPlayerInputComponent function) is made is not the same component that the character is responding to in-game. Let me know if you are able to remove the BMovementComponent and WeaponComponent pointers and instead use the movement component and skeletal mesh that are provided by ACharacter by default?

Cheers

Ah yes, I followed this: https://docs.unrealengine.com/latest/INT/Programming/Tutorials/Components/2/index.html to program my pawns movement. How would you suggest I go on about extending and using the existing movement component of the character instead of making a new one?
All I need is to modify the existing tickcomponent.

I’m still new to UE4, coming from UE3, and extending and referencing is still a bit iffy for me in UE4, any good sources and examples for me to use?

The default character movement component in Character.h is called “CharacterMovement” and the default skeletal mesh is just called “Mesh”. If your class inherits from ACharacter then you can use these names to refer to them.

If you’re looking for learning tools you can watch the tutorial videos on different aspects of the engine (A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums). You can also check out the programming guide in the documentation (https://docs.unrealengine.com/latest/INT/Programming/).

Can I just refer to the CharacterMovement component and modify the tickcomponent from my character, or do I have to extend it somehow?

I found out that if I attach a skeletal mesh to my character through the editor, it does indeed jump. However, it seems to not be properly attached to my character and slides around. And how come the jump only affects the mesh and not the entire character? Is the jumping in the sidescroller preset just visual?

I just found and fixed another issue, the tutorial for movement component I found used the pawn movement component instead of character movement component. I am now extending the appriopriate class.

However, when I try to use my controls now, nothing happens at all. Not even the left or right movement I programmed.

So I’ve figured out now that I am extending all the character classes instead of pawn classes, it uses the movement logic tied to the Capsule component (which was offset and stuck, since I wasn’t using it) instead of the custom movement logic created through the tutorials, tied to my new root component. So now I can jump, but not move.
BMovementComponent->AddInputVector() does absolutely nothing while extending from the character movement component.

How can I disable the default Capsule component and use my custom movement logic? I assume doing so would also disable jumping, making me have to program it from scratch, right?

I have added a new Brawler.rar for you to check out the changes.

I guess maybe I should go further up the hierarchy and not use character after all?
If so, I guess, I’m going back to my original question. How would I make a custom pawn (not character!) be able to fall? I guess the jump should be simple enough.

This is my idea so far, add an upwards impulse when jumping and set it to a falling state, while it’s in the falling state, give it a constant downwards force, when it collides with the ground, set it to the walking state. But this makes me also have to differentiate between the pawn colliding with walls/ceilings and a walkable floor, and detect when it falls off edges. Any quick, existing solutions for that?

Looking at the updated project, your character is still referring to the BMovementComponent in the function that controls side-to-side movement. Swapping this for GetCharacterMovement() instead allowed the character to move as well as jump.

Hmm, I tried doing that like this:

void ABrawler_Character_Player::BeginPlay()
{
	BMovementComponent = GetCharacterMovement();
	Super::BeginPlay();
}

void ABrawler_Character_Player::Move_YAxis(float AxisValue)
{
	BMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);

}

But it still will not move left and right.