Download

Checking if Player is in Camera View

Hi,

I’m a newbie and trying to make a security camera and i already did a way for checking the player with a Raycast to player position and it works well right now :


But problem is,that i’m trying to to do a second check for player position with a Camera to see if player is front of Camera or not.
I’ve found a way to check it with player camera in the UE answers :

bool ASecCamera::Check(AActor* Actor)
{
ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();

if (LocalPlayer != nullptr && LocalPlayer->ViewportClient != nullptr && LocalPlayer->ViewportClient->Viewport)
{
    FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(LocalPlayer->ViewportClient->Viewport,GetWorld()->Scene,LocalPlayer->ViewportClient->EngineShowFlags).SetRealtimeUpdate(true));

    FVector ViewLocation;
    FRotator ViewRotation;
    FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, ViewLocation, ViewRotation, LocalPlayer->ViewportClient->Viewport);
    if (SceneView != nullptr)
    {
        return SceneView->ViewFrustum.IntersectSphere(Actor->GetActorLocation(), Actor->GetSimpleCollisionRadius());    
    }
}

return false;

}

But as i said it just works with Player Camera and local player.

Is it possible to check it with another Camera to see if player is front of it for a amount of time ? i wasn’t able to change the upper code for this purpose as “CalcSceneView” only works with local player.

Hello let me see if i understand you, you want to detect a player in front of the camera in the air there?
You can ether make a capsule\ collision component and detect overlap.
Or you can do a trace from the camera actor you can run this several different ways.

The fastest is using the Actor Tick function but it is also the most expensive resource wise.
Also it a good idea to add a socket to the camera mesh that way you can trace from the lense a more accurately.
Also for a camera you may want a cone trace.

I don`t have a example for you atm but if you are having problems let me know.
WCode

Hi, I think you are over complicating this, why you don’t use dot product between you camera forward axis and your pawn?, like in the old school

Sure always more ways then one, :slight_smile: just how i would do it, that is all.

Yeah,the camera wasn’t supposed to be in the Air :smiley: at least not that high that was just a problem which happened when i added a Camera component to it and when i checked the editor camera was way lower than the place it was was supposed to be (maybe i wasn’t it attached to the main object).

And the Camera is going to rotate to left and right slowly (until it finds the player and stop at the place till player left it’s view).Not sure if overlap works this way.

And about the trace,i already did a line trace to player position but i want it just to check front of camera even if i do the trace to Camera forward vector it will just check a small place.
But this “cone trace” sounds great an example or help with how i can do this will be much appreciated !

Never was heard anything about dot product but i did as you said (i think) with getting Player location and camera forward vector like this :

float DotP = FVector::DotProduct(Player->GetActorLocation(),this->GetActorForwardVector());

GEngine->AddOnScreenDebugMessage(-1, 0.2, FColor::Red, (FString::Printf(TEXT(“D = %f”), DotP)));

Should i have to check the numbers and see if it’s between a specific numbers ?

I was did this in UNITY3D before with a raycast to player position and checking the camera planes and i was going to use this for AI too so they can’t find the player from behind for now i used a sphere component that rotates to player position and do a line trace to sphere’s forward vector.but if any of this works with both of them i’ll go with that.

And thanks for the answers both of you !

yes, dot product basically is the difference between two vectors and it has a lot of usability, and it’s a very old trick, for example in shaders you can create the must basic lambert with that, check my super illustration, red arrow is some arbitrary vector incoming, and green arrows are your vertex normals, if you get a dot product between your vertex and a light vector normalized, you will have values between 1 and -1, so!, same thinking can be applied to the security camera, if the camera is looking towards you, your value should be something between .7 or 1 depending on your aspect ratio, and boom you are in jail now!

here is the wiki for dot product: Dot product - Wikipedia

I did it with Dot product like this :


float DotP = FVector::DotProduct(Player->GetActorLocation(),this->GetActorForwardVector());
    GEngine->AddOnScreenDebugMessage(-1, 0.01, FColor::Red, (FString::Printf(TEXT("D = %f"), DotP)));

    if (FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) < 350&& FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) > 250) {
        if (DotP > -180 && DotP < 30) {
            GEngine->AddOnScreenDebugMessage(-1, 0.01, FColor::Red, (FString::Printf(TEXT("Player"))));
        }
    }

    if (FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) < 500 && FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) > 350) {
        if (DotP > -230 && DotP < 65) {
            GEngine->AddOnScreenDebugMessage(-1, 0.01, FColor::Red, (FString::Printf(TEXT("Player"))));
        }
    }

    if (FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) < 650 && FVector::Distance(Player->GetActorLocation(), this->GetActorLocation()) > 500) {
        if (DotP > -270 && DotP < 95) {
            GEngine->AddOnScreenDebugMessage(-1, 0.01, FColor::Red, (FString::Printf(TEXT("Player"))));
        }
    }

I’ve checked the distance to player so it will have smaller view at close distance (i think changing Camera height will get a problem like this).it works pretty well at front of camera but looks like it works behind the camera as well as “DotP” have same values behind the camera as it have front of it not sure if i did something wrong or what.

Thanks for the explains.

hi kratos, yes you should normalize your vectors, you will have values between 0 and 1, because you are proccesing directly your ActorLocation(), and that could be something big