I’ve been having this problem all day and for the life of me can’t seem to figure out what I could be missing. I’m attempting to add a simple space bar input to my pawn, where I press space, the pawn destroys itself and spawns in a “rocket”. Here is what I have so far:
MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class TESTINPUT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;
UPROPERTY(EditAnywhere)
int dir = 1;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void SpawnRocket();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Kismet/GameplayStatics.h"
#include "Rocket.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
//set mesh
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
if (CubeVisualAsset.Succeeded())
{
VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAction("SpawnRocket", IE_Released, this, &AMyPawn::SpawnRocket);
}
void AMyPawn::SpawnRocket()
{
Destroy();
ARocket* newRocket = GetWorld()->SpawnActor<ARocket>(GetActorLocation(), GetActorRotation());
newRocket->SetDir(dir);
}
And finally, the inputs inside the project settings.
I’m not sure what I could be missing? I set up the space bar input, created the spawn rocket function, bound the space bar to that function, and then finally had the player auto posses the pawn. What could I be missing? Any help is appreciated.