Getting my sliding door to work

Because I had a slightly different door, I used a different approach.

I got 8:53 so I haven’t watched the end yet but feeling quite proud of getting the code and the Lerp Interpolation to do what I want (I lowered the percentage for the video capture from 0.025 to 0.01).

Now to watch the end of this lecture. Probably there’s a way to condense or improve this somehow.



UPDATE#1: Tried to update it to just use the .Z value but I must have made a mistake because I corrupted the project and lost everything (it now doesn’t open).

I have to start again with everything. I’m going to try to use version control for my next attempt. At least I didn’t get too far with it.



UPDATE #2: I managed to get everything back bar adding the complex collision to the door (I think I might make a collision mesh for it shortly to keep it performant).

I think the previous reason it corrupted was because I tried to add an FVector into the header file and it didn’t have the correct class - GameFramework/Actor.h added for me to use FVector and it corrupted it with an AutoSave.

I was able to reduce the code a little:

ZVector = 300.0f;

FVector StartPoint = GetOwner()->GetActorLocation();
FVector EndPoint = FVector(StartPoint.X, StartPoint.Y, StartPoint.Z + ZVector);
FVector OpenDoor = FMath::Lerp(StartPoint, EndPoint, 0.01);
GetOwner()->SetActorLocation(OpenDoor);

This time I’ve created version control on it so I can revert it back to an older working version.



UPDATE#3 So after playing with a while and looking around my scene during play, i noticed that my door wasn’t really interpolating. Then after looking outside to see where my door was, I noticed it kept flying up into the air, which is obviously not what we want haha…

So I ended up initializing some variables in the header file and declaring them in Begin Play() similar to what Mike did. This meant that the End value didn’t keep adding the translation offset every frame. Now the interpolate lerp works properly using the float values declared in BeginPlay() as they are only calculated once.

My edited code:

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	StartZ = GetOwner()->GetActorLocation().Z;
	EndZ = StartZ + TransZ;
	
}


// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	FVector StartPoint = GetOwner()->GetActorLocation();
	FVector EndPoint = FVector(StartPoint.X, StartPoint.Y, TransZ);
	FVector OpenDoor = FMath::Lerp(StartPoint, EndPoint, 0.5 * DeltaTime);
	GetOwner()->SetActorLocation(OpenDoor);

}

This has been helpful in showing me exactly how the Tick portion of the Component code works and to be careful with it.



UPDATE#4 So I got my door to work with a trigger volume (now continuing with the older version of the course). I also managed to get the trigger volume to close the door to prevent the player from leaving the room.

if(PressurePlate->IsOverlappingActor(ActorThatOpens)) 
	{
		FVector StartPoint = GetOwner()->GetActorLocation();
		FVector EndPoint = FVector(StartPoint.X, StartPoint.Y, EndZ);
		FVector OpenDoor = FMath::Lerp(StartPoint, EndPoint, 1.5 * DeltaTime);
		GetOwner()->SetActorLocation(OpenDoor);
	}
	else
	{
		FVector StartPoint = GetOwner()->GetActorLocation();
		FVector EndPoint = FVector(StartPoint.X, StartPoint.Y, StartZ);
		FVector OpenDoor = FMath::Lerp(StartPoint, EndPoint, 5.0 * DeltaTime);
		GetOwner()->SetActorLocation(OpenDoor);
	}


1 Like

Privacy & Terms