# Movement in Player Controller

**URL:** <https://forums.unrealengine.com/t/movement-in-player-controller/371529>\
**Category:** Character & Animation\
**Tags:** unreal-engine, CPP, question\
**Created:** [September 25, 2016, 12:01am UTC](https://forums.unrealengine.com/t/movement-in-player-controller/371529 "2016-09-25T00:01:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FirstLight](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FirstLight](https://forums.unrealengine.com/u/FirstLight)\
**Post date:** [September 25, 2016, 12:01am UTC](https://forums.unrealengine.com/t/movement-in-player-controller/371529/1 "2016-09-25T00:01:15Z")

</div>

I recently started translating my Blueprints to C++ code to help with performance, and I’ve been stuck on this problem for a while: I used my Blueprint Player Controller to manage input, as I wanted input to be common to all player characters, but I didn’t want to bog down the character itself with the input management as the character could also be possessed by an AI controller. I tried to port that over in C++ using the SetupInputComponent, like this:

```
void APlayerUnitController::SetupInputComponent()
{
    if (InputComponent)
 {
	InputComponent->BindAxis("MovementY", this, &APlayerUnitController::MoveY);
	InputComponent->BindAxis("MovementX", this, &APlayerUnitController::MoveX);
 }
}

void APlayerUnitController::MoveY(float AxisValue)
{
    if (PlayerRef == NULL)
    {
	PlayerRef = Cast<APlayerUnit>(GetPawn());
    }
    else if((PlayerRef->bIsMovementEnabled) && (AxisValue != 0.0f))
    {
	const FRotator Rotation = ControlRotation;
	const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
	const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	//Stops AxisValue from going above 1 in either direction, which would speed up our character
	AxisValue = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
	PlayerRef->AddMovementInput(Direction, AxisValue);
    }
}

void APlayerUnitController::MoveX(float AxisValue)
{
    if (PlayerRef == NULL)
    {
	PlayerRef = Cast<APlayerUnit>(GetPawn());
    }
    else if ((PlayerRef->bIsMovementEnabled) && (AxisValue != 0.0f))
    {
	const FRotator Rotation = ControlRotation;
	const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
	const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
	//Stops AxisValue from going above 1 in either direction, which would speed up our character
	AxisValue = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
	PlayerRef->AddMovementInput(Direction, AxisValue);
        }
}

```

But so far nothing works, and I’m not sure what I’m doing wrong, seeing as most of it is code from the engine’s Third Person C++ template, adapted to a Player Controller. If anybody can tell me or provide some other way to make it work, I would be very grateful.

---

<div class="post-metadata">

**Author:** ![GigasightMedia](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigasightMedia](https://forums.unrealengine.com/u/GigasightMedia)\
**Post date:** [September 25, 2016, 5:36pm UTC](https://forums.unrealengine.com/t/movement-in-player-controller/371529/2 "2016-09-25T17:36:38Z")

</div>

Add a call to Super::SetupInputComponent before your binding actions in SetupInputComponent; or pull the functionality from APlayerController::SetupInputComponent into your overriding method. Creation of the InputComponent occurs there, so your InputComponent likely is never created, thus the if(InputComponent) check never passes.

See: Runtime/Engine/Private/PlayerController.h  
lines 2242 - 2256

The input component is created at line 2247

---

<div class="post-metadata">

**Author:** ![FirstLight](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FirstLight](https://forums.unrealengine.com/u/FirstLight)\
**Post date:** [September 25, 2016, 7:06pm UTC](https://forums.unrealengine.com/t/movement-in-player-controller/371529/3 "2016-09-25T19:06:37Z")

</div>

Oh…wow, I feel dumb! I can’t believe I forgot to do that, that made it work. Thanks a lot!
