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"));
}
}