# Rotating an object more than 180 degrees

Hey there!

I am trying to make a generic function to rotate an object a given angle. The problem is that when the “TargetRotation” = “InitialRotation” + “DeltaRotation” > 180, the object starts spinning without stopping.

Logging in the console the current rotation of the object I’ve realized that it goes from -180 to 180 degrees and then resets, so obviously it will never reach that “TargetRotation” when is greater than 180 or less than -180 degrees.

I’ve tried checking if this “TargetRotation” is greater than 180 and decreasing it by 360, then calculating how many total rotations would be needed, but it doesn’t seem to work.

Is there any way to overcome this and get a function that rotates an object a given angle around one axis?

The easiest solution is to just attach it to a parent component and use SetRelativeRotation instead.

For example create a scene component and make the static mesh a child of it.

It would mean you would need to find the static mesh component from the owner.

Managed to solve it by taking a different approach:

The problem was asking the object for its current rotation, that is where things got confusing and went from -180 to 180 degrees, so I decided to just remove that feedback, since I am sure what it’s current rotation is, as I am setting it by myself.

Code:

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

Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

// Open Door
if (PressurePlate && PressurePlate->IsOverlappingActor(TriggeringActor)) {
RotateObject(DeltaYaw, 2, DeltaTime);
}

// Close Door
else if (PressurePlate && !PressurePlate->IsOverlappingActor(TriggeringActor)) {
RotateObject(0, 2, DeltaTime);
}

}

void UOpenDoor::RotateObject(float DeltaValue, float speed, float DeltaTime) {

TargetRotation.Yaw = InitialRotation.Yaw + DeltaValue;

//CurrentRotation.Yaw = FMath::Lerp(CurrentRotation.Yaw, TargetRotation.Yaw, speed);
//CurrentRotationMap[axis] = FMath::FInterpConstantTo(CurrentRotationMap[axis], TargetRotationMap[axis], delta_time, 45);
CurrentRotation.Yaw = FMath::FInterpTo(CurrentRotation.Yaw, TargetRotation.Yaw, DeltaTime, speed);

GetOwner()->SetActorRotation(CurrentRotation);

}
``````

This way, “DeltaYaw” can be set to whatever angle and the object rotates correctly.

• Syntax

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