Download

How to implement simple controlled movement along X- and Y-axis?

Hello, my first post here!

I’d like to start off by saying I have little to no logic experience with Unreal Engine. I’m an artist with intermediate programming skills.

I’ve spent a few days making really pretty materials and building a scene - no problems. Then I hit a brickwall trying to add controls to an object. I tried to read up on some documentation, but it ended up confusing me even more. Could you please explain step by step how should I go about making simple controlled physics-less wasd-movement on x- and y-axis’ from scratch? Think Hotline Miami-like controls. I have a vague understanding of what Game Mode does, but it’s not really getting me anywhere.

Thank you :- )

I’m gonna try to bump this. Please forgive me, but I’m in need of help :- (

Am I really asking for too much?

I am working on a very simple endless runner in which the player is a cube and an endless tunnel is generated. The player (cube) can not move forward (X direction, though this depends on your setup) only up and down, left and right. The tunnel moves in the X direction which makes it appear that the player is moving forward endlessly. Those details should help you to understand the logic below, and the code should be simple enough that it can be translated to blueprints, which I’m assuming is what you are using.

If you want to clamp movement at a certain position like I did, you should avoid using hard coded numbers like my code below. I plan on changing them :). Same with the speed (400). These should be stored in variables so they are easier to change.

The speed is multiplied by DeltaTime which is provided by the tick function. You will see an output for it in the Blueprint. DeltaTime is the time for each frame, and so by multiplying the speed by it, the speed will be the same at 5fps as it is at 60fps. I would try to google this subject for a more detailed explanation and why you never want to forget it.

As you can see, I use SetActorLocation to do the actual moving. I am not sure if this is the best way to do it, but I believe it is more than ideal for a non-physics based movement system.

This code should be setup inside your Pawn Blueprint.

Let me know if you have any questions :slight_smile:



void AMyCharacter::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	if (GetWorld()->GetFirstPlayerController()->IsInputKeyDown(EKeys::A) && GetActorLocation().Y > -242)
	{
		SetActorLocation(FVector(GetActorLocation().X, GetActorLocation().Y - (400 * DeltaTime), GetActorLocation().Z), true, nullptr, ETeleportType::None);
	}

	if (GetWorld()->GetFirstPlayerController()->IsInputKeyDown(EKeys::D) && GetActorLocation().Y < 242)
	{
		SetActorLocation(FVector(GetActorLocation().X, GetActorLocation().Y + (400 * DeltaTime), GetActorLocation().Z), false, nullptr, ETeleportType::None);
	}

	if (GetWorld()->GetFirstPlayerController()->IsInputKeyDown(EKeys::S) && GetActorLocation().Z > -242)
	{
		SetActorLocation(FVector(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z - (400 * DeltaTime)), false, nullptr, ETeleportType::None);
	}

	if (GetWorld()->GetFirstPlayerController()->IsInputKeyDown(EKeys::W) && GetActorLocation().Z < 242)
	{
		SetActorLocation(FVector(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z + (400 * DeltaTime)), false, nullptr, ETeleportType::None);
	}
	
}


You could also do it in the Player Controller Blueprint to simplify getting the key down. If you do it in the Player Controller Blueprint, you’ll need to get a reference to the Pawn to set its location. It might be daunting at first, but just search in the blueprint editor for the function names I used in the code above, and it should all start to make sense :slight_smile:

Thank you for your reply! It’s highly appreciated, altho not exactly what I was looking for. OP wasn’t worded correctly, my bad. My issue is that it looks like I can’t just directly apply movement logic, or any logic for that matter, to an object, but instead have to tinker with GameModes - if I’m understanding all the tutorials correctly. After stiching together the Pawn Blueprint, what do I do with it?

Not to be rude, but I would really recommend going through the documentation and/or the beginner blueprint tutorials on Youtube. Look up the unreal youtube channel for the massive playlist, and you’ll find a section about blueprints. Unfortunately I think that question is a little too much at the beginner level, and it must be understood before you can make more progress. Official tutorials and documentation should get you on the right track, and then you can reference the actual movement logic I posted above

Essentially, the game mode controls what player controller blueprint (or c++ class) is used for the player controller, same with the pawn, game state, etc. If you create an empty blueprint template, you will need to create your own game mode blueprint, set it as the default game mode, and then inside that blueprint, assign your own player controller, pawn, etc.

An easier solution to get started perhaps would be to create a new project from the third person template. You will then see that the game mode uses a third person character as the pawn, and there is a blueprint for it you can immediately edit. You could delete the nodes inside that pawn and then implement simple movement, but it would use the unreal guy (grey humanoid) so it might look silly. It would let you dive right in without messing with the game mode though.

EDIT: an extremely simple way to see that your own code (blueprint node) is running is to create a third person project, and inside the third person character blueprint, add a print string node at the end of any execution path, and when you play the game, you should see that string printed when you do whatever it’s connected to (such as moving)

Thank you, no offence taken! I tried to force myself through the documentation but it at the very least seemed to me like it skipped explaining the bits I missed. It would seem like I was wrong.

Thank you for explaining this. I really appreciate it :- )
It might be just me, but maybe this could be put out there at the very beginning of the documentation - explainin gwhy you need to split logic between these two, and only then proceed to show how to use them.

No you might actually be right. It’s been a long time since I have watched those videos or read the documentation, but I think in the videos they show you how to do simple blueprint things by modifying existing blueprints such as the third person character, which would not require you to create a custom game mode and choose a different pawn for that game mode.

SirMehMcMeow, would you like to see a video tutorial on how to do this? I have always wanted to do a tutorial on something so I can do this for you if you would like :smiley: