The simplest UE 4 C + + tutorial -- door opening effect through interpolation and overlapping events [XXXI]

design sketch

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

Original English address

In this tutorial, in this fantasy engine 4 C + + tutorial, we will learn how to automatically open a door by using the lerp function and overlapping events according to the player's direction. Create a new actor class, such as OpenDoorWithLerp.

First, in h file, let's use #include BoxComponent at the top of the file. Make sure it appears in the {generated. Field of the Actor h) before the document.

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

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

#include "OpenDoorWithLerp.generated.h"

Next, we will create variables. We will declare the UStaticMeshComponent, UBoxComponent of the door, our overlap function, bool, float, and the starter variable for door rotation.

Three bool variables determine the state of the door, and four float variables set different numbers for the door. Next, we will add functions for switching doors and use the UStaticMeshComponent and UBoxComponent that build the door itself. All elements will be placed under the common part 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()
	void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

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

	bool Open;
	float RotateValue;
	FRotator DoorRotation;

Next, we will enter the of Actor cpp file. We first #include the kismetmathlibrary header file. We will use a mathematical function in the overlapping function.

#include "Kismet/KismetMathLibrary.h"

In the constructor, we will set the default variable. We first set the Open (bool type) of the door to false. Next, we will set up our UBoxComponent and ustatic meshcomponent. 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.0f, 50.0f, -50.0f));
    Door->SetupAttachment(RootComponent);

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

In the Tick function, we will check whether the door is open and run the lerp function. You must run a 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 we get the rotation of the door, we will move yaw to 90, - 90, or 0 smoothly.

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

	DoorRotation = Door->RelativeRotation;

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

}

Next, let's create overlapping functions. OnOverlapBegin will first conditionally check the null value to determine whether the function should continue. The function then checks the direction in which players and actors face based on their position and rotation. In this function, our player, that is, our Pawn, is the OtherActor parameter passed to the function. We subtract Pawn's position from actor's position 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 content example of illusion engine 4. If the player is in front of the door, RotateValue will be equal to 90.0f. If not, RotateValue will be equal to - 90.0f. Then, finally, we set Open to true.

Onverlapend 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();
        FVector Direction = GetActorLocation() - PawnLocation;
        Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation());

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

        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;
    }
}

We have finished the code. Enter the editor and compile. Drag and drop characters into the game world. Set the BoxComponent's collision preset to Trigger, and add the door static mesh from the launcher content as the UStaticMeshComponent.

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

Keywords: UE4 unreal

Added by xcmir on Wed, 05 Jan 2022 09:04:05 +0200