Download

UPawnMovementComponent returning null?

Hi all, I’m very new to c++ with ue4 so this might have an easy solution but I’m having trouble getting my pawn to move like a character would. I added a pawn movement component but it keeps returning null and not running my add input vector code. If there is an easier way to make my pawn move it would be nice to know or if I can fix this code so my movement component can work properly that would also be great. Here is my code in my PlayerPawn.cpp:

**#include “PlayerPawn.h”

#include “Components/StaticMeshComponent.h”
#include “GameFramework/SpringArmComponent.h”
#include “Camera/CameraComponent.h”
#include “GameFramework/PawnMovementComponent.h”

#include “Engine.h”
#include “GameFramework/Controller.h”

// Sets default values
APlayerPawn::APlayerPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;


// Create a root component
SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RootComponent = SceneComp;

// Create a static mesh component for a visual representation of the player
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
StaticMesh->SetupAttachment(RootComponent);

// Create a spring arm component
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->TargetArmLength = 500.0f;    // The camera follows at this distance behind the pawn
SpringArm->bUsePawnControlRotation = true;    // Rotate the arm based on the pawn

// Create a camera component
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
Camera->bUsePawnControlRotation = false;    // Camera does not rotate relative to the arm

// Create an instance of the movement component, and tell it to update our root component
OurMovementComponent = CreateDefaultSubobject<UPawnMovementComponent>(TEXT("MovementComponent"));

// Auto possess the player 
AutoPossessPlayer = EAutoReceiveInput::Player0;

}

// Called when the game starts or when spawned
void APlayerPawn::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void APlayerPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void APlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);

PlayerInputComponent->BindAxis("MoveForward", this, &APlayerPawn::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerPawn::MoveRight);

PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);

}

void APlayerPawn::MoveForward(float Value)
{

if ((Controller) && (Value != 0.0f))
{
    // 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);

    if ((OurMovementComponent) && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(Direction * Value);
    }
}

}

void APlayerPawn::MoveRight(float Value)
{
//CurrentVelocity.Y = FMath::Clamp(Value, -1.0f, 1.0f) * 1000.0f;

if ((Controller) && (Value != 0.0f))
{
    // Find out which way is right
    const FRotator Rotation = Controller->GetControlRotation();
    const FRotator YawRotation(0, Rotation.Yaw, 0);

    // Get right vector
    const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

    AddMovementInput(Direction, Value);

    if ((OurMovementComponent) && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(Direction * Value);
    }
}

}**

My game runs with no errors but my pawn just doesn’t want to move.
Any help is much appreciated, thank you :slight_smile:

I forget exactly but I think pawnmovment is just a base class that doesnt do much. There is floatingpawnmovement. But honestly you should make all your pawns a character (yes even creatures) unless you really know what you are doing it will save you a ton of pain in your project.

I think I will just change my pawns to character’s because I’ve encountered many other problems that characters would easily fix. Thanks so much for the help :slight_smile: