judge the position of an actor relative to the player pawn

I was wandering how to judge if an actor is on the left or on the right of the player?

The dot product is your friend, the following should work for any kind of actor not just players. Untested and uncompiled.


/**
 * return true if Direction is left of Rotation
 * NOTE: Direction does NOT have to be normalized
 */
bool IsLeftOrRight(FRotator Rotation, FVector Direction)
{
	FVector RightVector = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);

	//TODO: handle == 0.0f: neither left or right but either directly in front or behind; will also be == 0.0f if Direction is == FVector::ZeroVector (RightVector should always be normalize because of FRotationMatrix)
	//this is the dot product (operator|)
	return (RightVector | Direction) < 0.0f;
}

/**
 * return true if OtherLocation is left of Location/Rotation
 */
bool IsLeftOrRight(FVector Location, FRotator Rotation, FVector OtherLocation)
{
	FVector LocationToOtherLocation = OtherLocation - Location;

	return IsLeftOrRight(Rotation, LocationToOtherLocation);
}

bool IsLeftOrRight(AActor* Player, AActor* Other)
{
	//TODO: check Player and Other != nullptr
	return IsLeftOrRight(Player->GetActorLocation(), Player->GetActorRotation(), Other->GetActorLocation());
}