Alternative Solution?

Hi there,

I came up with a slightly different solution that results in my door revolving constantly if I set the InitialYaw to above ~130 degrees. Anyone have any ideas as to why?

void UOpenDoor::BeginPlay()
{
    Super::BeginPlay();
    InitialYaw = GetOwner()->GetActorRotation().Yaw;
    TargetYaw = InitialYaw + TargetYaw;
}

// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
    
    UE_LOG(LogTemp, Warning, TEXT("%s"), *GetOwner()->GetActorRotation().ToString());
    UE_LOG(LogTemp, Warning, TEXT("Yaw: %f"), GetOwner()->GetActorRotation().Yaw);
    
    CurrentYaw = GetOwner()->GetActorRotation().Yaw;
    FRotator OpenDoor(0.f, TargetYaw, 0.f);
    OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 2);
    GetOwner()->SetActorRotation(OpenDoor);
}
1 Like

Iā€™ve edited your post to use a code block, please do that in the future.

GetActorRotation returns a value between -180 and 180 so if itā€™s at 130 and then do +90 that would go to 220 which is unreachable.

You could use GetDenormalized() on the FRotator so it has a value of 0-360 but youā€™ll run into similar ssues with different rotations.


The easiest way to solve this is to use SetRelativeRotation on the static mesh. So you would have an actor with a scene component which will be used to for the rotations to be relative to as SetRelativeRotation is based off of the root component e.g.

SceneComponent
|-StaticMeshComponent
|-OpenDoorComponent

And then you would need to get that static mesh component in code and then youā€™d have something like

InitialYaw = FMath::FInterpTo(InitialYaw, OpenAngle, DeltaTime, 2);
DoorMesh->SetRelativeRotation(FRotator(0.f, InitialYaw, 0.f));
2 Likes

Hi Dan,

Thanks for replying so fast. I now understand why it happened (thanks again), but Iā€™m struggling to understand your solution. Iā€™m not sure what you mean by ā€œscene componentā€. I donā€™t know what ā€œwhich will be used to for the rotations to be relative toā€ means or how to ā€œuseā€ SetRelativeRotation on the static mesh (a static mesh is just the model or frame that textures go onto, right?), Iā€™ve never seen/used ā€œ|ā€ before. Sometimes I struggle to fully grasp some of the concepts in the lectures, have I done that here?

Itā€™s a derived type of UActorComponent

A SceneComponent has a transform and supports attachment, but has no rendering or collision capabilities. Useful as a ā€˜dummyā€™ component in the hierarchy to offset others.

Source from the docs

That half explains it. So if that component is the root, and the door mesh is a child of that then if you were to do +90 SetRelativeRotation on the door mesh it would always open 90 outwards as itā€™s relative to the root component.

Say you rotate the door in your scene to be (0, 270, 0) that would be the rotation of your root component. So with the static mesh as a child of that you can say rotate +90 relative to that.

How have you been getting components so far? :slight_smile:

Not really. I tend to be a bit vague on purpose as a lot of people like figuring things out themselves and I donā€™t want to rob people of that by just going ā€œhereā€™s the full solutionā€.
So I start off with general advice on how to go about something and gauge on how much direction and guidence is needed based on replies.

Start off by dragging an Empty Actor into your world and then add a static mesh component to it

1 Like

hi

Just tried to have one of the initial yaw =140ā€¦
and used
GetOwner()->SetActorRotation(DoorRotation);
and it worked as it is supposed to be when i set the target to 90 . Are you sure about the 180 limit ??? Because when the door is fully open the Current Yaw shows 230 as it is supposed to be;

This is the partial code i used in TickComponent.() function which is similar to Mikeā€™s

        FRotator DoorRotation = GetOwner()->GetActorRotation();
	CurrentYaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 2.0f); 
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

And here it is how it looks ; (also added on screen messages to the values)

Iā€™m saying that GetActorRotation only returns values within that range. Not that you canā€™t pass in an FRotator with values outside of that to SetActorRotation.

1 Like

ok. now looking into it more detailed ā€¦so GetActorRotation() normalizes the values , and that is why you mentioned about using GetDenormalized() on FRotator; makes sense now :slight_smile: Thanks ā€¦that is very good information indeed.

But I believe his issue is because of updating the CurrentYaw() with the GetActorRotation() rather than Lerp() to FInterpTo() function. That is the difference in mine and it gets me the right value to rotate rather than Normalized().

Another issue I noticed (I was originally doing)
if we set SetActorRotation({0.0f, CurrentYaw, 0.0f});
then this might give some unexpected results in the future for different cases because while we are updating yaw value we are also setting the other axises to zero and if they are not then it might be issue.
Not on this one because the other axis rotations are already zero so I think interfacewise the solution that Mike used is a good one or the one you suggested with using Parent - Child relation and SetRelativeRotation(). Maybe DoorFrame being the parent since the door is attached to it . But I could never think of the empty actor way you describedā€¦

I tried that Parent - Child relation with the DoorFrame ->Door but I think I messed up since I was not sureā€¦how to access values. I believe we are gonna cover those in the upcoming sectionsā€¦

Thatā€™s what I was saying, probably just worded it weirdly :confused:.

ToonTanks goes over creating components in code very early on.

1 Like

no you worded correctly ā€¦I just jumped in too fast :slight_smile:

thanks it is awesome info because doing this extra work teaches a lot more on top :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms