I’ve been trying to go about getting this to function.
I’m trying to get a blank project and do complete C++ rather than touching blueprints for understand. I want to get a good feel for the code in UE4 rather than visual scripting.
Using the links at end, I’ve found some understanding on moving the character, but uncertain how I would go about getting the mouse to do Mouse X and the movement to be C++ forward/backward.
So, Mouse X would control rotation around the Y axis via Mouse X and the W and S keys would perform a simple forward and backward movement based on the current forward direction.
If an answer seems to easy, perhaps even some guides to more reference of the camera portion would assist me in this task.
Did look at the template. Seems it shows the concepts I’m looking for, but the actual implementation is confusing me (sorry, new with UE). From what I gather, it’s as easy as input mapping, then assigning binds. But I can’t seem to get the W/S or Forward/Backward to work accordingly based on the forward Vector3. I know the template accomplishes this, but I want to be able to accomplish this from scratch.
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
Alright. I’ll try to explain what all of this does in a simple way. First and foremost we have a constant FRotator named Rotation. We want this to get the currently controlled rotation by using Controller->GetControlRotation();.
Secondly we’re using another constant FRotator this time it’s named YawRotation because it is going to be using…well, the yaw movement! X,Y,Z coordinate system and all of that jazz. We’re going to give it the variables 0, the Rotation we created earlier and specifically the .Yaw portion and last but not least we’ll set the last variable to 0. We’re only interested in the yaw as of right now.
Then we want to get the forward vector as the commented code says. So we make an FVector variable named Direction and we assign it to the result of FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
Last but not the least the actual movement itself. AddMovementInput which is what I believe you want. It comes from APawn which your character should be inheriting from. Call the function with the FVector world direction, in this case it is called Direction and then assign it a value. AddMovementInput(Direction, Value);
The value is assigned in the editor itself when mapping the keys. When that key is pressed it will call the function and the value you set in the editor will respond appropriately.
void AThePlanetCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
// Set up gameplay key bindings
InputComponent->BindAxis("MoveForward", this, &AThePlanetCharacter::MoveForward);
}
I can understand wanting to do everything by hand and by scratch but it isn’t really necessary when alot of the tools are available for you right from the get-go. There isn’t a need to make it any harder for yourself. Also, I don’t use the blueprints (Albeit they would make things easier and really the Unreal team is awesome for making it!) since I prefer to make everything by code. But… you’re going to learn quickly that you need to work with the editor and not necessarily against it. Side by side making code and using the editor to change things around as well. Trust me on that.
TL;DR
AddMovementInput is what you want if you’re going to do it by scratch. Make sure you’re inheriting from pawn. If you still run into any snags then follow this or send me a message on the forums. Good luck!
This is perfect, as a note:
I wasn’t inheriting from APawn, which was simply a mistake, and getting forward was the main issue. Nailed down two issues with one answer. Cheers.