문을 움직이게 해보자.
//...
UCLASS()
class BASIC_API AFloorSwitch : public AActor
{
//...
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void RaiseDoor();
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void LowerDoor();
};
//...
void AFloorSwitch::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Overlap Begin."));
RaiseDoor();
}
void AFloorSwitch::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
UE_LOG(LogTemp, Warning, TEXT("Overlap End."));
LowerDoor();
}
문이 z축으로 날아간다.
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloorSwitch.generated.h"
UCLASS()
class BASIC_API AFloorSwitch : public AActor
{
GENERATED_BODY()
public:
/**
* Overlap volume for functionality to be triggerd
*/
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Floor Switch")
class UBoxComponent* TriggerBox;
/**
* Switch for the character to stop on
*/
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Floor Switch")
class UStaticMeshComponent* FloorSwitch;
/**
* Door to move
*/
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Floor Switch")
UStaticMeshComponent* Door;
UPROPERTY(BlueprintReadWrite, Category = "Floor Switch")
FVector InitialDoorLocation;
UPROPERTY(BlueprintReadWrite, Category = "Floor Switch")
FVector InitialSwitchLocation;
public:
// Sets default values for this actor's properties
AFloorSwitch();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void RaiseDoor();
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void LowerDoor();
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void RaiseFloorSwitch();
UFUNCTION(BlueprintImplementableEvent, Category = "Floor Switch")
void LowerFloorSwitch();
UFUNCTION(BlueprintCallable, Category = "Floor Switch")
void UpdateDoorLocation(float Z);
UFUNCTION(BlueprintCallable, Category = "Floor Switch")
void UpdateFloorSwitchLocation(float Z);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloorSwitch.h"
#include <Engine/Classes/Components/BoxComponent.h>
#include <Engine/Classes/Components/StaticMeshComponent.h>
// Sets default values
AFloorSwitch::AFloorSwitch()
{
// 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;
TriggerBox = CreateDefaultSubobject<UBoxComponent>(TEXT("TriggerBox"));
RootComponent = TriggerBox;
FloorSwitch = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Switch"));
FloorSwitch->SetupAttachment(GetRootComponent());
Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
Door->SetupAttachment(GetRootComponent());
TriggerBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
TriggerBox->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
TriggerBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
TriggerBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
TriggerBox->SetBoxExtent(FVector(62.f, 62.f, 32.f));
}
// Called when the game starts or when spawned
void AFloorSwitch::BeginPlay()
{
Super::BeginPlay();
TriggerBox->OnComponentBeginOverlap.AddDynamic(this, &AFloorSwitch::OnOverlapBegin);
TriggerBox->OnComponentEndOverlap.AddDynamic(this, &AFloorSwitch::OnOverlapEnd);
InitialDoorLocation = Door->GetComponentLocation();
InitialSwitchLocation = FloorSwitch->GetComponentLocation();
}
// Called every frame
void AFloorSwitch::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AFloorSwitch::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Overlap Begin."));
RaiseDoor();
LowerFloorSwitch();
}
void AFloorSwitch::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
UE_LOG(LogTemp, Warning, TEXT("Overlap End."));
LowerDoor();
RaiseFloorSwitch();
}
void AFloorSwitch::UpdateDoorLocation(float Z)
{
FVector NewLocation = InitialDoorLocation;
NewLocation.Z += Z;
Door->SetWorldLocation(NewLocation);
}
void AFloorSwitch::UpdateFloorSwitchLocation(float Z)
{
FVector NewLocation = InitialSwitchLocation;
NewLocation.Z += Z;
FloorSwitch->SetWorldLocation(NewLocation);
}