I’m trying to make a small glass fly towards the player. The glass manages to raise up along the Z axis, but then the engine just crashes with no error messages. Am I doing something badly in my code, which could be causing the crash?
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "DOMUSCharacter.h"
#include "FlyingJunkEnemy.generated.h"
UCLASS()
class DOMUS_API AFlyingJunkEnemy : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFlyingJunkEnemy();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UFUNCTION(BlueprintCallable, Category = "Events")
void StartJunkMovementAnim();
private:
bool junkmovementBegin;
bool notAtTarget;
bool doOnce;
ACharacter* playerCharacter;
UBoxComponent* BoxComponent;
FVector junkStartLocation;
FVector temp;
FVector distanceVector;
FVector playerLoc;
FVector junkLoc;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "DOMUS.h"
#include "FlyingJunkEnemy.h"
// Sets default values
AFlyingJunkEnemy::AFlyingJunkEnemy()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//init bool used in movement anim
junkmovementBegin = false;
//bool to check if junk is at target (player)
notAtTarget = true;
doOnce = false;
temp = this->GetActorLocation();
distanceVector = { 0.0f, 0.0f, 0.0f };
playerLoc = { 0.0f, 0.0f, 0.0f };
junkLoc = { 0.0f, 0.0f, 0.0f };
//get player character
playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
//get player location as FVector (do this dynamically as needed, as value will change)
//playerCharacter->GetActorLocation();
//get starting junk enemy location
junkStartLocation = this->GetActorLocation();
//create root component as cube with collision
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
RootComponent = BoxComponent;
BoxComponent->InitBoxExtent(FVector(5.0f, 5.0f, 5.0f));
BoxComponent->SetCollisionProfileName(TEXT("Pawn"));
}
// Called when the game starts or when spawned
void AFlyingJunkEnemy::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFlyingJunkEnemy::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
//run when StartJunkMovementAnim function is called from blueprint
if (junkmovementBegin)
{
if (this->GetActorLocation().Z < (junkStartLocation.Z + 300))
{
//do this until junk moves 30 up Z axis
temp = { this->GetActorLocation().X, this->GetActorLocation().Y, this->GetActorLocation().Z + 0.2f };
this->SetActorLocation(temp);
}
else if (notAtTarget)
{
//doonce
if (!doOnce)
{
playerLoc = { playerCharacter->GetActorLocation().X, playerCharacter->GetActorLocation().Y, 0.0f };
junkLoc = { junkStartLocation.X, junkStartLocation.Y, 0.0f };
distanceVector = junkLoc - playerLoc;
distanceVector.Normalize();
distanceVector.X *= 0.5f;
distanceVector.Y *= 0.5f;
doOnce = true;
}
//each frame
this->SetActorLocation(this->GetActorLocation() + distanceVector);
if (FMath::Abs(this->GetActorLocation().X - playerCharacter->GetActorLocation().X) < 200)
{
if (FMath::Abs(this->GetActorLocation().Y - playerCharacter->GetActorLocation().Y) < 200)
{
notAtTarget = false;
}
}
}
else
{
junkmovementBegin = false;
BoxComponent->SetSimulatePhysics(true);
}
}
}
void AFlyingJunkEnemy::StartJunkMovementAnim()
{
//begin movement defined in 'Tick' function
junkmovementBegin = true;
}