Can't seem to move character with AddMovementInput

I’ve created a Character blueprint, which means it automatically has a CharacterMovementComponent attached to it. Then I’ve added a custom C++ class that extends the UActorComponent. I setup my input bindings and verified that they work through logging. Since I want to move in the direction my camera is facing I tried

FVector forward = GetWorld()->GetFirstPlayerController()->PlayerCameraManager->GetCameraRotation().Vector();
//val is the parameter passed in from the input axis binding
Cast<ACharacter>(GetOwner())->AddMovementInput(forward, val);

I’m logging both forward and val to the screen and when I press up (the input axis I bound), both values are non-zero but my character doesn’t move anywhere.

Hello, Thundercleez

To implement the required functionality, you can do the following:

  1. Add a C++ class, which extends UInputComponent. The declaration will look something like this:

    #include “Components/InputComponent.h”
    #include “MyInputComponent.generated.h”

    UCLASS()
    class MOVEQUESTION_API UMyInputComponent : public UInputComponent
    {
    GENERATED_BODY()

    public:

    UMyInputComponent(const class FObjectInitializer& ObjectInitializer);
    
    //handler for forward movement
    UFUNCTION()
    void MoveForward(float Val);
    
    //other input handlers
    

    };

Implementation in .cpp file:

 #include "MoveQuestion.h"
    #include "MyInputComponent.h"
    
    UMyInputComponent::UMyInputComponent(const class FObjectInitializer& ObjectInitializer) 
    	:Super(ObjectInitializer) {
    	//Binding forward input handler in the constructor of your InputComponent
    	BindAxis("MoveForward", this, &UMyInputComponent::MoveForward);
    	//Binding other input handlers
    }

//Implementation of MoveForward() function
void UMyInputComponent::MoveForward(float Value)
{
	const FVector Direction = GetWorld()->GetFirstPlayerController()->PlayerCameraManager->GetCameraRotation().Vector();
	Cast<ACharacter>(GetOwner())->AddMovementInput(Direction, Value);
}

2 Add a property for InputComponent to your Character class:

UPROPERTY(VisibleDefaultsOnly, Category = Movement)
	UMyInputComponent* InputHandlerComponent;

3 Initialize the component in constructor of your Character:

InputHandlerComponent = ObjectInitializer.CreateDefaultSubobject<UMyInputComponent>(this, TEXT("InputComponent"));

This should do it. Also please don’t forget to select your Character as default Pawn class in your GameMode class/blueprint.

Hope this helped!

Thanks for the detailed response but I’m still a little confused. Are you saying AddMovementInput only works if called from an InputComponemt? My character is also a blueprint, so I can’t add any properties to it as mentioned in step 2. Finally, is step 3 necessary if I add the InputComponemt C++ class through the Add Component button that is available after opening a blueprint in the editor?

I also had thought an InputComponemt was automatically added to a character blueprint, so I had thought adding a 2nd one would screw things up.

As for your question about AddMovementInput – it is not actually like that. This function is defined in APawn; thus, it should be called for objects that are instances of APawn class or classes, derived from it. In the example, it is called for ACharacter, which derives from APawn (note that we cast the class, that is received from GetOwner() function to ACharacter).
Regarding the implementation of step 2 for blueprint, please do the following:

  1. Open you Character blueprint in Blueprint editor.
  2. In the Components tab (top right corner of the screen by default), click on Add Component and select New C++ Component.
  3. Click on Show All Classes flag and start typing the name of InputComponent.
  4. Press Create Class and save the Blueprint.
  5. In Visual Studio, implement appropriate functionality (described in steps 1 and 2 in my first answer).

As for the third step, you shouldn’t do it in this case, as the required component is already in the blueprint.
And finally, as for automatic addition of InputComponent – this would be at some point logical assumption if there were no other ways to handle input for Character. However, it is not so, since input functionality can be implemented solely in Character class.

Good luck!

Well I finally found the issue. At some point I must have unchecked auto activate in the CharacteMovement component because it was unchecked. After checking it, I can finally move!