Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by sanghai · Sep 20, 2011 at 01:34 PM · cameracamera-movementcamera.maincamera-scrolling

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sanghai · Sep 21, 2011 at 06:25 AM 0
Share

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 ?

avatar image asafsitner · Sep 21, 2011 at 08:27 AM 1
Share

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.

avatar image sanghai · Sep 21, 2011 at 09:20 AM 0
Share

Attaching colliders to the camera ? can u explain me in detail or any link where i can get the example ? ....thanks in advance ..

avatar image flamy · Jan 23, 2012 at 04:54 AM 1
Share

did you try with changing it from world to viewport coordinates?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make the camera stop moving after a gameobject is destroyed? 1 Answer

How to start with camera and Airplane for the game ? 1 Answer

Camera Follow (Not Exact) 1 Answer

Need help for camera view like Air Strike 3d game ? 1 Answer

I can not find Cinemachine in package manager. There is no option of 'All Assets in package manager.' 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges