I'm having trouble with basic pawn movement in C++(I'm losing my mind)

Hi everyone. So for the past week I have been having issues making my pawn move CORRECTLY. I’ve looked at multiple different examples but can’t get it exactly how I want.

I’ve tried using SetActorLocation() and had success in making my pawn move but the issue is that it has absolutely no collisions and it passes through everything.

I’ve tried using the AddMovementInput() but it doesn’t affect the position of my StaticMesh pawn at all although the inputs are going through.

I’ve tried using AddActorLocalOffset() but it makes my Pawn spiral and glitch out of control.

I’ve tried using the PawnMovementComp’s AddInputVector() but it didn’t cause any kind of movement.

I really don’t know how to get a desired 2D topdown movement while retaining collision. I’m losing my mind. Here is my code for reference. I am just using the SetActorLocation method right now because at least it moves right? I do not even know if this is efficient and any leads as to what I should do would be much appreciated.

#include "FuzzyChefPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/PawnMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
AFuzzyChefPawn::AFuzzyChefPawn()
{
	// 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;
	
	AutoPossessPlayer = EAutoReceiveInput::Player0;

	//Creates capsule collision as the root component
	CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComponent"));
	CapsuleComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	RootComponent = CapsuleComponent;
	CapsuleComponent->InitCapsuleSize(42.f, 96.0f);
	//Create static mesh component(empty)
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	Mesh->SetupAttachment(RootComponent);
	// Create a camera boom (pulls in towards the player if there is a collision)
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);
	//CameraBoom->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	CameraBoom->TargetArmLength = 800.0f; // The camera follows at this distance behind the character	
	CameraBoom->bUsePawnControlRotation = false; // Rotate the arm based on the controller
	CameraBoom->RelativeRotation = FRotator(-60.f, 0, 0);
	 // Create a follow camera
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
	FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
	//Movement Component being added
	ChefMovementComponent = CreateDefaultSubobject<UPawnMovementComponent>(TEXT("MovementComp"));
	ChefMovementComponent->UpdatedComponent = RootComponent;

	//Critical Variables
	
	
}

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

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

	FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
	SetActorLocation(NewLocation);

}

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

	check(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &AFuzzyChefPawn::VerticalMovement);
	PlayerInputComponent->BindAxis("MoveRight", this, &AFuzzyChefPawn::HorizantalMovement);

}

void AFuzzyChefPawn::VerticalMovement(float value)
{
	if (Controller != NULL)
	{
		CurrentVelocity.X = value * VerticalMoveSpeedMultiplier;	

		UE_LOG(LogTemp, Warning, TEXT("Vert"));
	}
}

void AFuzzyChefPawn::HorizantalMovement(float value)
{
	if (Controller != NULL)
	{
		CurrentVelocity.Y = value * HorizantalMoveSpeedMultiplier;

		UE_LOG(LogTemp, Warning, TEXT("Horiz"));
	}
}
1 Like

Some questions:

  • Why are you creating a new MovementComponent (ChefMovementComponent) when APawn already has one declared? (I’m assuming you’re inheriting from APawn?)
  • Are you sure that the Pawn is not moving without the mesh?
  • Is it critical to use a static mesh? You could also convert your static mesh to skeletal and use the Character class, or hide the Character class’ mesh and attach the static mesh there?

Thanks for the reply.

I’m quite new to all of this, especially C++ so any tips are much appreciated.

I tried using the move comp. from APawn but it didn’t work so I just ended up adding another to see if it worked and it didn’t so I should probably remove that.

I don’t get the second question. If you mean, whether the pawn and the Mesh(I assigned it a random cube) are moving with the current code then, yes they are moving but have no collision.

I didn’t really weigh the pros and cons of using static over skeletal. My model I plan to use doesn’t have any bones but should I still use skeletal?

And final thing, how would I use the move comp. from the APawn correctly while maintaining collision?

Thanks for the reply!

Thank you! I’ll try it tomorrow morning and tell you how it goes!

This is the correct way to assign custom movement component