Math
Explore essential Unreal Engine 5 math functions for C++ development. Learn about Min, Max, Ceil, Floor, Absolute, Clamp, Lerp, Vector, Rotator operations, and random number generation with UE5 C++ examples.
Unreal Engine 5 Math Functions
This section covers essential math operations available in Unreal Engine 5's C++ environment, primarily utilizing the KismetMathLibrary
and FMath
classes. These functions are crucial for game logic, physics calculations, and general programming tasks within UE5.
Integer Comparison: Min and Max
Determine the smaller or larger of two integer values.
Min (Int)
#include "Kismet/KismetMathLibrary.h"
int32 A;
int32 B;
int32 Min = UKismetMathLibrary::Min(A, B);
Max (Int)
#include "Kismet/KismetMathLibrary.h"
int32 A;
int32 B;
int32 Max = UKismetMathLibrary::Max(A, B);
Floating-Point Comparison: Min and Max
Find the minimum or maximum value between two floating-point numbers.
Min (Float)
#include "Kismet/KismetMathLibrary.h"
float A;
float B;
float Min = UKismetMathLibrary::FMin(A, B);
Max (Float)
#include "Kismet/KismetMathLibrary.h"
float A;
float B;
float Max = UKismetMathLibrary::FMax(A, B);
Rounding and Absolute Value
Operations for rounding numbers and finding their absolute magnitude.
Ceil
Rounds a float up to the nearest integer.
float A;
int32 Result = FMath::CeilToInt(A);
Floor
Rounds a float down to the nearest integer.
float A;
int32 Result = FMath::FloorToInt(A);
Absolute
Returns the absolute value of an integer or float.
int32 A;
int32 Result1 = FMath::Abs(A);
float B;
float Result2 = FMath::Abs(B);
Value Clamping and Interpolation
Constrain values within a range and smoothly transition between them.
Clamp
Constrains a value to be within a specified minimum and maximum range.
float A;
float Min;
float Max;
float Result = FMath::Clamp(A, Min, Max);
Lerp (Linear Interpolation)
Linearly interpolates between two floats based on an alpha value.
float A;
float B;
float Alpha;
float Result = FMath::Lerp(A, B, Alpha);
Nearly Equal
Checks if two float values are approximately equal within a given tolerance.
float A;
float B;
float ErrorTolerance;
bool Result = FMath::IsNearlyEqual(A, B, ErrorTolerance);
Vector and Rotator Operations
Creating, breaking, and manipulating vectors and rotators.
Make Vector
Constructs a new FVector
from X, Y, and Z components.
float X;
float Y;
float Z;
FVector ExampleVector(X, Y, Z);
Make Rotator
Creates a new FRotator
from Pitch, Yaw, and Roll values.
float Pitch;
float Yaw;
float Roll;
FRotator ExampleRotator(Pitch, Yaw, Roll);
Break Vector
Access the individual X, Y, and Z components of an FVector
.
You can simply access each variable of a FVector by using .X
, .Y
and .Z
.
FVector ExampleVector;
ExampleVector.X; // X
ExampleVector.Y; // Y
ExampleVector.Z; // Z
Break Rotator
Retrieve the Pitch, Yaw, and Roll components from an FRotator
.
You can simply access each variable of a FRotator by using .Pitch
, .Yaw
and .Roll
.
FRotator ExampleRotator;
ExampleRotator.Pitch; // Pitch
ExampleRotator.Yaw; // Yaw
ExampleRotator.Roll; // Roll
Vector Length
Calculate the magnitude (length) of a vector.
FVector ExampleVector;
ExampleVector.Size();
Distance (Vector)
Compute the Euclidean distance between two points represented by vectors.
FVector A;
FVector B;
FVector::Distance(A, B);
Normalize (Vector)
Adjust a vector to have a magnitude of 1 while retaining its direction.
FVector ExampleVector;
float Tolerance;
// Normalizes the vector in-place
ExampleVector.Normalize(Tolerance);
Interpolation Functions
Smoothly interpolate between values over time.
FInterp To
Linearly interpolates a float value towards a target value.
float Current;
float Target;
float DeltaTime;
float InterpSpeed;
float Result = FMath::FInterpTo(Current, Target, DeltaTime, InterpSpeed);
VInterp To
Linearly interpolates a vector towards a target vector.
FVector Current;
FVector Target;
float DeltaTime;
float InterpSpeed;
FVector Result = FMath::VInterpTo(Current, Target, DeltaTime, InterpSpeed);
RInterp To
Linearly interpolates a rotator towards a target rotator.
FRotator Current;
FRotator Target;
float DeltaTime;
float InterpSpeed;
FRotator Result = FMath::RInterpTo(Current, Target, DeltaTime, InterpSpeed);
Orientation and Direction
Calculate rotations based on spatial relationships.
Find Look at Rotation
Determines the rotation needed to look from a start point towards a target point.
#include "Kismet/KismetMathLibrary.h"
FVector Start;
FVector Target;
FRotator Result = UKismetMathLibrary::FindLookAtRotation(Start, Target);
Random Number Generation
Generate random integers and floats within specified ranges.
Random Integer
Generates a random integer between 0 and the specified maximum value (inclusive).
#include "Kismet/KismetMathLibrary.h"
int32 Max;
int32 Result = UKismetMathLibrary::RandomInteger(Max);
Random Float
Generates a random float between 0.0 and 1.0.
#include "Kismet/KismetMathLibrary.h"
float Result = UKismetMathLibrary::RandomFloat();
Random Integer in Range
Generates a random integer within a specified minimum and maximum range (inclusive).
#include "Kismet/KismetMathLibrary.h"
int32 Min;
int32 Max;
int32 Result = UKismetMathLibrary::RandomIntegerInRange(Min, Max);
Random Float in Range
Generates a random float within a specified minimum and maximum range.
#include "Kismet/KismetMathLibrary.h"
float Min;
float Max;
float Result = UKismetMathLibrary::RandomFloatInRange(Min, Max);