Hi guys, I am trying to create an object indicator that should indicate the player is looking near to some actor. Indicator is the widget that is in the middle of the screen and it changes its opacity. Lets assume I have a box and the player is standing in front of it. If the player (POV) will turn into the box and will look close to any side of it (some small value) the indicator will increase its opacity and when he hits the box it will have full opacity. This should work from all sides of the box the same way and player can stand on any side of the box. Thanks for help
Get Unit Direction from box to player. Dot product that vector against player’s forward. You’ll get a -1 <=> 1 range.
This should work, although I haven’t tested it. It returns 0 if the player is facing a direction perpendicular to the actor and approaches 1 as the player begins to directly face the actor.
float AYourPlayerCharacter::GetFacingActorIntensity(AActor* TargetActor)
{
// Get the player's forward vector
FVector PlayerForward = GetActorForwardVector();
// Get the direction vector from the player to the target actor
FVector PlayerToActor = TargetActor->GetActorLocation() - GetActorLocation();
PlayerToActor.Normalize();
// Calculate the dot product between the players forward vector and the direction to the target actor
float DotProduct = FVector::DotProduct(PlayerForward, PlayerToActor);
// Calculate the facing intensity based on the dot product
float FacingIntensity = FMath::Clamp(DotProduct, 0.0f, 1.0f);
return FacingIntensity;
}
Run this function in the Tick and use the return value to modify the opacity of the widget.