I made Both door opening

// Fill out your copyright notice in the Description page of Project Settings.


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

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

	// ...
}


// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	// Intializing the TargetYaw according to it's Initial Location
	
	TargetYaw = GetOwner()->GetActorRotation().Yaw + 90.f;
}


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

	

	float CurrentYaw = GetOwner()->GetActorRotation().Yaw;

	FRotator Rotation (0.f,FMath::FInterpTo(CurrentYaw,TargetYaw,DeltaTime,2),0.f);
	
	GetOwner()->SetActorRotation(Rotation);

	UE_LOG(LogTemp, Warning , TEXT("Value of Yaw is : %s "),*GetOwner()->GetActorRotation().ToString());

	// ...
}

So the Header file is as it is shown in lecture but I didn’t initialize it. What I did here, is I initialized the TargetYaw in BeginPlay() but adding 90 from it’s initial value. And code is working perfectly.

One More Thing, When I used “FInterpConstantTo” instead of “FInterpTo”, the second door ( The one which is not attached with wall in Video) kept rotating. may be that is just with me or some Unreal Glitch.

I’ve had the same problem as you when using FInterpConstantTo and there is a way to fix this. I’ll first point those who are interested on the right direction but if you just want the solution just read until the end :slight_smile:

The Problem
At first I thought it was a bug on Unreal just like the OP originally thought but after further investigation, it’s a fault with our approach in terms of how we are implementing the rotation with FInterpTo and FInterpConstantTo. The problem lies on how FRotator is implemented. If you refer to UE4’s Documentation (https://docs.unrealengine.com/en-US/API/Runtime/Core/Math/FRotator/index.html), you’ll see that FRotator is implemented in a particular way that glitches out if your initial yaw is somewhere between 90 and 179 degrees.

The Solution
I honestly recommend you to investigate for yourself first as I literally learned loads on how UE4 handles different things by investigating this one problem, it will help you MASSIVELY if you try to sort this out for yourself.

Moving forward, if you already tried to sort this out and couldn’t notice the problem then here it is:

From the documentation I linked above you’ll see that Yaw is described as “Rotation around the up axis (around Z axis), Running in circles 0=East, +North, -South.

Noticed that I put the last part in bold as that is our problem when Rotating as we both did on our solutions :stuck_out_tongue:
Yaw 0 means that the object is facing East. With this in mind, try setting your Yaw in the engine to 180 degrees or even 230 degrees just for argument sake and then print this out on UE_LOG. You’ll see that the value is Negative!

If your initial Yaw is set to 90 and you’re adding your target yaw as you did:
TargetYaw = GetOwner()->GetActorRotation().Yaw + 90.f;
then your target will be 180.f but the engine will automatically translate the object’s final rotation to -180.

FInterpConstantTo will be constatly trying to reach 180 degrees but it will never reach that as we’re passing a negative value when we finally get to our target yaw.

To fix this you’ll need to use two additional functions in FRotator and the name of these funcions are:
FRotator::ClampAxis() This one will convert your value range from [-180.f, 180.f] to [0.f, 360.f]
FRotator::NormalizeAxis() This one will convert your value range from [0.f, 360.f] to [-180.f, 180.f]

So here’s the solution in code:

void UDoorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	float CurrentYaw = FMath::FInterpConstantTo(FRotator::ClampAxis(CurrentRotation.Yaw), TargetYaw, DeltaTime, 45.f);
	CurrentRotation.Yaw = FRotator::NormalizeAxis(CurrentYaw);
	GetOwner()->SetActorRotation(CurrentRotation);
}

And you could also Clamp the TargetYaw too just for consistency in the code:

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

	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	TargetYaw = FRotator::ClampAxis(CurrentRotation.Yaw + 90.f);
}

I hope this helps :slight_smile:

Privacy & Terms