I was just messing around a bit when it suddenly crashed and i couldn’t start it again. Always crashes at 71%.
Code.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “GameFramework/Pawn.h”
#include “Components/BoxComponent.h”
#include “Kismet/GameplayStatics.h”
#include “Spaceshooter2.h”
#include “ShipController.generated.h”
UCLASS()
class SPACESHOOTER2_API AShipController : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn’s properties
AShipController();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere)
UShapeComponent* CollisionBox;
UPROPERTY(EditAnywhere)
float Speed = 10.0f;
UPROPERTY(editanywhere,
Category = "Spawning")
TSubclassOf<class ABulletController>
BulletBlueprint;
void Move_XAxis(float AxisValue);
void Move_YAxis(float Axisvalue);
void OnShoot();
FVector CurrentVelocity;
bool Died;
UFUNCTION()
void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor
* OtherActor, UPrimitiveComponent* OtherComponent, int32
OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
code.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include “ShipController.h”
#include “BulletController.h”
#include “Spaceshooter2.h”
#include “EnemyController.h”
#include “Kismet/GameplayStatics.h”
// Sets default values
AShipController::AShipController()
{
// 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;
CollisionBox = CreateDefaultSubobject<
UBoxComponent>(TEXT("ROOT"));
CollisionBox->bGenerateOverlapEvents = true;
CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &
AShipController::OnOverlap);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AShipController::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AShipController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!CurrentVelocity.IsZero()) {
FVector NewLocation = GetActorLocation() +
Speed * CurrentVelocity * DeltaTime;
SetActorLocation(NewLocation);
}
}
// Called to bind functionality to input
void AShipController::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAxis("MoveX", this, &
AShipController::Move_XAxis);
InputComponent->BindAxis("MoveY", this, &
AShipController::Move_YAxis);
InputComponent->BindAction("Shoot", IE_Pressed,
this, &AShipController::OnShoot);
}
void AShipController::Move_XAxis(float AxisValue)
{
CurrentVelocity.X = AxisValue * 100.0f;
}
void AShipController::Move_YAxis(float AxisValue)
{
CurrentVelocity.Y = AxisValue * 100.0f;
}
void AShipController::OnShoot()
{
UWorld* World = GetWorld();
if (World)
{
FVector Location = GetActorLocation();
World->SpawnActor<ABulletController>
(BulletBlueprint, Location, FRotator::
ZeroRotator);
}
}
void AShipController::OnOverlap(UPrimitiveComponent*
OverlappedComponent, AActor* OtherActor, UPrimitiveComponent*
OtherComponent, int32 OtherBodyIndex, bool bFromSweep, const
FHitResult& SweepResult)
{
if (OtherActor->IsA(AEnemyController::StaticClass()))
{
Died = true;
this->SetActorHiddenInGame(true);
UGameplayStatics::SetGamePaused(GetWorld(), true);
}