My Code for opening and closing the door

I added an additional param in OpenDoor(float DeltaTime,float angle)

and this is my full code

.h file
*> *
> #pragma once
*> *
> #include “CoreMinimal.h”
> #include “Components/ActorComponent.h”
> #include “Engine/TriggerVolume.h”
*> *
*> *
> #include “OpenDoor.generated.h”
*> *
> #pragma region
> UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
> class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
> {
> GENERATED_BODY()
*> *
> public:
> // Sets default values for this component’s properties
> UOpenDoor();
*> *
> protected:
> // Called when the game starts
> virtual void BeginPlay() override;
> *
> public:
> // Called every frame
> virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction ThisTickFunction) override;

*> *
> private:
> void OpenDoor(float DeltaTime,float angle);
> *
> float InitialYaw;
> float CurrentYaw;
> *
> UPROPERTY(EditAnywhere,Category = “My Custom Category”)
> float TargetYaw = 90.0f;
> UPROPERTY(EditAnywhere)
> ATriggerVolume PressurePlate;

> UPROPERTY(EditAnywhere)
> AActor ActorThatOpen;

*> *
> };
> #pragma endregion OpenWorld Class

.cpp file

#include "OpenDoor.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"
#include "Engine/World.h"

#pragma region
// Sets default values for this component’s properties
UOpenDoor::UOpenDoor()
{
*** // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features***
*** // off to improve performance if you don’t need them.***
*** PrimaryComponentTick.bCanEverTick = true;***

*** // …***
}
#pragma endregion Constructor

#pragma region ***
void UOpenDoor::BeginPlay()
{
*** Super::BeginPlay();

*** InitialYaw = GetOwner()->GetActorRotation().Yaw;***
*** CurrentYaw = InitialYaw;***
*** TargetYaw += InitialYaw;***

*** if (PressurePlate == nullptr)***
*** {***
*** UE_LOG(LogTemp, Error, TEXT(“NO Pressure Plate Assined in %s gameObject”), GetOwner()->GetName());**
*** }***

*** ActorThatOpen = GetWorld()->GetFirstPlayerController()->GetPawn();***

}
#pragma endregion BeginPlay

#pragma region
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction ThisTickFunction)*
{
*** Super::TickComponent(DeltaTime, TickType, ThisTickFunction);***

*** if (PressurePlate != nullptr && PressurePlate->IsOverlappingActor(ActorThatOpen))***
*** OpenDoor(DeltaTime, TargetYaw);***
*** else***
*** OpenDoor(DeltaTime, InitialYaw);***
}
#pragma endregion TickComponent

#pragma region ***
void UOpenDoor::OpenDoor(float DeltaTime,float angle)
{
*** CurrentYaw = FMath::Lerp(CurrentYaw, angle, 0.02f);

*** FRotator DoorRotation = GetOwner()->GetActorRotation();***
*** DoorRotation.Yaw = CurrentYaw;***
*** GetOwner()->SetActorRotation(DoorRotation);***
}
#pragma endregion OpenDoor

// in .cpp file

#include “OpenDoor.h”
#include “GameFramework/Actor.h”
#include “GameFramework/PlayerController.h”
#include “Engine/World.h”

#pragma region
// Sets default values for this component’s properties
UOpenDoor::UOpenDoor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don’t need them.
PrimaryComponentTick.bCanEverTick = true;

// ...

}
#pragma endregion Constructor

#pragma region
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();

InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
TargetYaw += InitialYaw;

if (PressurePlate == nullptr)
{
	UE_LOG(LogTemp, Error, TEXT("NO Pressure Plate Assined in %s gameObject"), *GetOwner()->GetName());
}

ActorThatOpen = GetWorld()->GetFirstPlayerController()->GetPawn();

}
#pragma endregion BeginPlay

#pragma region
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

if (PressurePlate != nullptr && PressurePlate->IsOverlappingActor(ActorThatOpen))
	OpenDoor(DeltaTime, TargetYaw);
else
	OpenDoor(DeltaTime, InitialYaw);

}
#pragma endregion TickComponent

#pragma region
void UOpenDoor::OpenDoor(float DeltaTime,float angle)
{
CurrentYaw = FMath::Lerp(CurrentYaw, angle, 0.02f);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
}
#pragma endregion OpenDoor

in .h

#pragma once

#include “CoreMinimal.h”
#include “Components/ActorComponent.h”
#include “Engine/TriggerVolume.h”

#include “OpenDoor.generated.h”

#pragma region
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()

public:
// Sets default values for this component’s properties
UOpenDoor();

protected:
// Called when the game starts
virtual void BeginPlay() override;

public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

private:
void OpenDoor(float DeltaTime,float angle);

float InitialYaw;
float CurrentYaw;

UPROPERTY(EditAnywhere,Category = "My Custom Category")
float TargetYaw = 90.0f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
AActor* ActorThatOpen;

};
#pragma endregion OpenWorld Class

Privacy & Terms