- Home /
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 ?
Answer by flamy · Jan 20, 2012 at 12:51 PM
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!
Answer by raz899 · Feb 17, 2018 at 10:37 AM
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;
}
Answer by JmD_2 · Sep 20, 2011 at 03:02 PM
You might want to check up
http://unity3d.com/support/documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html
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.
///JmD
Yes I read few notes on Frustrum Planes it is confusing ... Still I will have a look with it...... is there any other way to do that ?
I will provide the image what issue I am facing
In below image those lines which are less dark is the maximum Horizontal extent to which my object can move but my camera angle is kind of V shape which is in dark Lines ...Now I am not able to go that place which is beyond the maximum horizontal extent....
any Soultion ?
Have you tried attaching colliders to your camera to limit movement? You can sort them into layers to make sure enemies can go beyond the screen limit but the player can't etc.
Attaching colliders to the camera ? can u explain me in detail or any link where i can get the example ? ....thanks in advance ..
did you try with changing it from world to viewport coordinates?