Components
Explore essential Unreal Engine 5 components for game development. Learn to play animations, set relative and world locations, and manage rotations with UE5 C++ examples.
Unreal Engine 5 Components
Play Animation
.h File
private:
// The animation asset needs to be set in blueprint
UPROPERTY(EditAnywhere, meta = (AllowPrivateAccess = "true"))
class UAnimationAsset* ExampleAnimation;
.cpp File
USkeletalMeshComponent* ExampleComponent;
if (ExampleAnimation)
{
ExampleComponent->PlayAnimation(
ExampleAnimation, // Animation asset
false // Loop animation?
);
}
Set Relative Location
USkeletalMeshComponent* ExampleComponent;
FVector NewLocation;
ExampleComponent->SetRelativeLocation(NewLocation);
Set World Location
USkeletalMeshComponent* ExampleComponent;
FVector NewLocation;
ExampleComponent->SetWorldLocation(NewLocation);
Set Relative Rotation
USkeletalMeshComponent* ExampleComponent;
FRotator NewRotation;
// Convert FRotator to FQuat and set relative rotation
ExampleComponent->SetRelativeRotation(FQuat::MakeFromRotator(NewRotation));
Set World Rotation
USkeletalMeshComponent* ExampleComponent;
FRotator NewRotation;
// Convert FRotator to FQuat and set world rotation
ExampleComponent->SetWorldRotation(FQuat::MakeFromRotator(NewRotation));