Is there any way to keep game object withing the limit of Camera FOV

I am trying to make game like AirStrike3D where my Airplane along with camera will move in the Z direction…camera will also scroll along X axis a little bit say 500 meter or something ( so that i get more space for object to be placed ) and I have coded for the airplane like where I have given var horizontal extent beyond which airplane cant go …Camera is at 55 degree to the terrain and it scrolls left n right as per the distance between Camera and object (( camera.position - object.position) >scrolldistance) then move camera ). NOw I have given a horizontal extent for the Airplane so that it cannot go beyond certain limit but this gives problem when i move my airplane forward and go to left even though there is space left but my airplane cannot go beyond limit …If i increase this vertical extent then when i m at initial position i.e. near the camera it will go out of screen … I hope i have succeded it in explainig or visualising the problem I am facing

NOW is there any possibility that code can be written for plane say Plane can not go beyond camera’s FOV ?

alt text

i have a simple solution for you, take the position of the object tht you want to keep inside the frustum of the camera and change it from world point to screen point using this following function.

if it is negative on x or y it is out of the screen on left or bottom respectively. and if it is more than 1 on x or y, then it is out of the screen on right or top respectively.

Since you know when it goes out you can restrict it from moving when the value turns negative or more than one! Hope it is clear!

This should work.

private void ClampPlayerToFrustrum()
    Ray ray  = Camera.main.ViewportPointToRay(new Vector3(0.0f, 0.0f, 0));
    Ray ray2 = Camera.main.ViewportPointToRay(new Vector3(1.0f, 0.0f, 0));
    Ray ray3 = Camera.main.ViewportPointToRay(new Vector3(0.0f, 1.0f, 0));
    Ray ray4 = Camera.main.ViewportPointToRay(new Vector3(1.0f, 1.0f, 0));

    RaycastHit hit, hit2, hit3, hit4;

    float offset = 2.0f;
    float rightLimitation = transform.position.x;
    float leftLimitation  = transform.position.x;
    float downLimitation  = transform.position.z;
    float upLimitation    = transform.position.z;

    if (Physics.Raycast(ray, out hit)){
        downLimitation = hit.point.z + offset;
    if (Physics.Raycast(ray2, out hit2)){
        rightLimitation = hit2.point.x - offset;
    if (Physics.Raycast(ray3, out hit3)){
        leftLimitation = hit.point.x + offset;
    if (Physics.Raycast(ray4, out hit4)){
        upLimitation = hit4.point.z - offset;

    Vector3 pos = transform.position;
    pos.x = Mathf.Clamp(transform.position.x, leftLimitation, rightLimitation);
    pos.z = Mathf.Clamp(transform.position.z, downLimitation, upLimitation);
    transform.position = pos;

You might want to check up

This lets you calculate the frustum (or at least frustum planes) of a camera. The frustum is the “pyramid” inside which objects are visible to the camera.