Download

Trying to understand input movement in C++

Hi guys, I’ve been following one guide from the wiki. ( https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial ). This guide helps you to recreate the first person shooter c++ template from ue4. This far I understand everything I’ve done following the guide but a few things related to the input.
My problem is with the MoveForward function and the jump function. They’re here https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial#UFUNCTION.28.29_Macro . I’m not pasting the code directly because I’m on my phone sorry guys.
Well first the MoveForward function, it needs one argument but when you call it in the input section you don’t pass any value to it. Does it take the value of the axis and pass it to the function automatically? Also I don’t understand what pitch is, I understand what you do to it, if you are on the ground or falling it’s limited but what is it and why do we have to limit it? And the main problem with this function, I don’t understand the line that says FRotationMatrix… No idea what it does and what matrix is :/. My last question about this, could I change the speed of my character just multiplying “Value” ? Like value *= 2??

Now my problem with the jumping function https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial#ActionMappings .
I understand what it does, but how a variable makes yo jump? I’m assuming a function on acharacter or apawn base classes takes that variable as an argument and it makes my character jump but I don’t know if I’m right. Can anyone explain what is happening when this variable changes to true? And how could I tweak the values to make my character jump higher for example. Also if anyone can explain me how could I make my own jump function just for learning purposes?
So, this was really long, hope I didn’t make you bored and thank you for the help.

Passing values to MoveForward(float value): Yes. IIRC in PlayerController, you bind the MoveForward axis to MoveForward() function, so it passes in the value which is between -1 and 1. Where -1 is moving full speed backward and 1 is moving full speed forward.

Limiting Pitch: pitch is your character looking up/down by moving the mouse forward/back. If you didn’t limit pitch, then you could push on the mouse and keep rotating the character’s head 360 degrees (like an airplane).

Rotation Matrix: this is a mathematical matrix that represents your characters rotation in the world. Here’s a good linear algebra tutorial:

http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

Change speed of character: yes, you can do it exactly like you said. Here’s sample code:



/**
* move speed as % of character default walk speed
*/
const float MOVE_SPEED = 0.6f;


/**
* handles character moving forward backward
*/
void ASimCharacter::MoveForward(float value)
{
	if (value != 0.0f)
	{
		// find out which way is forward
		FRotator Rotation = Controller->GetControlRotation();

		// limit pitch when walking or falling
		if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
		{
			Rotation.Pitch = 0.0f;
		}

		// add movement in that direction
		const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
		AddMovementInput(Direction, value * MOVE_SPEED);
	}
}

Let me start by saying. that was a great reply by JockoJonson.
I just want to add a little about how movement actually works.

The thing about movement in a 3d world, always remember its based on a vector…
But input is not so much, when you push a key on a keyboard it is a key press or value in range…
So, now you need to calculate all this based on what you know… or what you can figure out… IE the position of the players forward etc…

When you add all this together, you end up with a direction vector that can be used to move a object in 3dspace… basically object position vector + directional vector + deltatime (time used to make sure math calculations stay in sync with hardware)
If you forgot to add delta time, your character movement could change based on pc hardware and all your calculations would be awful…

When all is said in done though, you can move the character vector to its new position based on input.

Hope that at least helps some with what is actually happening in the character controller.

Thank you so much guys, that really solved all the doubts I had about moveforward function. Hope you can help me with the jumping function if ppssible.

Ok, well you can do this two ways…
One the hardway… create your own character controller from start to finish… a night mirror you will be living for months if you go down this path… gravity, etc will all be up to you to control.

Another way, take advantage of the hard work that Unreal has done and use their character controller and then override what you want to change.
To do this, you create your own class based on it and then in its constructor add your properties…

For example, can add some stuff to its base properties… can even access some of this in the engine blueprint properties… etc

Lots of stuff here can be changed… can dig into the ACharacter/CharacterMovement class and dig in deep if you want.
Overall though, what you really want to d o I think is just add the ability to jump.

In order to do this, you also will want to override the input class of the charactercontroller.

Something like this…

to the h files…

too the cpp file…

→ Edit…
I just looked at the code you are working with in more detail…
In short, you don’t need to worry about adding the math together because your using the character controller but knowing it may be useful.

It looks like in the example above, they use their own jump flag… but I am not sure why its needed…
They just seem to be setting it… maybe this is a old way or something… but a better way would be…

To change this
3. In SetupPlayerInputComponent, add the following:

and instead add the following

Calling the function on the ACharacter directly without setting its variable and waiting for it to notice it.
and maybe ignore the rest of the bPressedJump stuff and rem it out or not put it in.

Maybe I didn’t explain my doubts well sorry. My doubt is, in the code from that guide, the jump function just changes a variable, and my question is how the change of a variable without calling any other function in the code, makes you jump. Althought I find your answer very usefull, and makes me ask myself another question x). What is actually happening when you call super?

I try to answer your question in two parts…

One, When you call SUPPER, it calls the inherited function you are over riding.

Now, in your case when your talking about a character controller… your now looking at a much more advanced class…
But the same things are happening… code is either used or not used depending on if SUPER is called.

In the original code, I think they are trying to change variable in the inherited class and then hoping it gets picked up and works.
I dont think that’s the best method… In the edited step I added instead I call the function out of the inherited class.

I also dont understand the reason to do it… I guess they exposed this as a bool outside when its inherited… could maybe be a old way of doing the same thing… might not even work anymore… just call the function directly like I did in the example I showed.

You guys helped me so much. Thank you I don’t have any doubts now. This won’t last much tho xD