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

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

set_relative_location

USkeletalMeshComponent* ExampleComponent;

FVector NewLocation;

ExampleComponent->SetRelativeLocation(NewLocation);

Set World Location

set_world_location

USkeletalMeshComponent* ExampleComponent;

FVector NewLocation;

ExampleComponent->SetWorldLocation(NewLocation);

Set Relative Rotation

set_relative_rotation

USkeletalMeshComponent* ExampleComponent;

FRotator NewRotation;

// Convert FRotator to FQuat and set relative rotation
ExampleComponent->SetRelativeRotation(FQuat::MakeFromRotator(NewRotation));

Set World Rotation

set_world_rotation

USkeletalMeshComponent* ExampleComponent;

FRotator NewRotation;

// Convert FRotator to FQuat and set world rotation
ExampleComponent->SetWorldRotation(FQuat::MakeFromRotator(NewRotation));