rendering

 

 

The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

In this tutorial, in this unreal engine 4 C++ tutorial, we will learn how to automatically open a door depending on the player’s direction by using lerp functions and overlapping events. Create a new Actor class, such as OpenDoorWithLerp.

First, in the.h file, let’s use #include BoxComponent at the top of the file. Make sure it appears before the generated. H file in Actor.

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"

// include before generated file
#include "Components/BoxComponent.h"

#include "OpenDoorWithLerp.generated.h"
Copy the code

Next, we will create variables. We’ll declare the door’s UStaticMeshComponent, UBoxComponent, our overlapping function, bool, float, and the FRotator variable for the door rotation.

Three bool variables determine the state of the door, and four float variables set different numbers for the door. Next, we’ll add functions to toggle the door and use the UStaticMeshComponent and UBoxComponent that build the door itself. All elements will be placed under the common section of the header file.

.public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere)
	UStaticMeshComponent* Door;

	UPROPERTY(EditAnywhere)
	UBoxComponent* MyBoxComponent;

	// declare overlap begin function
	UFUNCTION(a)void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	// declare overlap end function
	UFUNCTION(a)void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

	bool Open;
	float RotateValue;
	FRotator DoorRotation;
Copy the code

Next, we’ll go into the Actor.cpp file. We’ll start with the #include KismetMathLibrary header. We’re going to use a mathematical function in the overlap function.

#include "Kismet/KismetMathLibrary.h"
Copy the code

In the constructor, we will set the default variable. We start by setting the door Open (type bool) to false. Next, we’ll set up our UBoxComponent and UStaticMeshComponent. We set UBoxComponent to our RootComponent. Then, connect the overlapping function to the UBoxComponent. Later, we will create the overlapping functions they are calling.

AOpenDoorWithLerp::AOpenDoorWithLerp()
{
 	// 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;

	Open = false;

    MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("My Box Component"));
    MyBoxComponent->InitBoxExtent(FVector(50.50.50));
    RootComponent = MyBoxComponent;

    Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("My Mesh"));
    Door->SetRelativeLocation(FVector(0.0 f.50.0 f.50.0 f));
    Door->SetupAttachment(RootComponent);

    MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapBegin);
    MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapEnd);
}
Copy the code

In the Tick function, we will check if the door is open and run the LERP function. You must run an LERP function in the Tick function. We will get the rotation of the Door by using Door->RelativeRotation to return the rotation of the Door on each frame. After getting the rotation of the door, we will smoothly move yaw to 90, -90, or 0.

void AOpenDoorWithLerp::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	DoorRotation = Door->RelativeRotation;

    if(Open)
    {
        Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0 f, RotateValue, 0.0 f)), 0.01 f));   
    } 
    else
    {
        Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0 f.0.0 f.0.0 f)), 0.01 f)); }}Copy the code

Next, let’s create the overlapping function. OnOverlapBegin will first perform a condition check on null values to determine whether the function should continue. This function then checks which direction the player and actor are facing based on their position and rotation. In this function, our player, our Pawn, is the OtherActor parameter passed to the function. We subtract the position of the Actor from the Pawn to get a direction FVector. Then we need to consider the rotation of the parent component, so we run UKismetMathLibrary: : LessLess_VectorRotator. This method is taken from the unreal Engine 4 content example. If the player is in front of the door, RotateValue will equal 90.0F, if not, RotateValue will equal -90.0F. And then, finally, we set Open to true.

OnOnverlapEnd simply sets Open to false.

void AOpenDoorWithLerp::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    if( (OtherActor ! =nullptr) && (OtherActor ! =this) && ( OtherComp ! =nullptr ) ) 
    {
        FVector PawnLocation = OtherActor->GetActorLocation(a); FVector Direction =GetActorLocation() - PawnLocation;
        Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation());

        if(Direction.X > 0.0 f)
        {
            RotateValue = 90.0 f;
        }
        else
        {
            RotateValue = 90.0 f;
        }

        Open = true; }}void AOpenDoorWithLerp::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    if( (OtherActor ! =nullptr) && (OtherActor ! =this) && ( OtherComp ! =nullptr ) )  
    {
        Open = false; }}Copy the code

We’ve done the code. Go to the editor and compile. Drag and drop the character into the game world. Set the collision default of the BoxComponent to Trigger and add a static mesh of doors from the initiator content as the UStaticMeshComponent.

Then you can get the same effect as at the beginning of the article