Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Erik-Sombroek · Jul 21, 2015 at 03:20 PM · cameraboundscamera movement

map boundaries for perspective camera

Hi,

I'm building a 2.5D game, where the perspective camera is looking to the side of the level and the characters can only move on the X and Y direction. I'm having a very hard time trying to get the camera not to leave the boundries of the map. The camera is free to move over the X and Y axis and can zoom in and out. What i managed to do is;

             Vector3 downRightEdgeScreen = Camera.main.WorldToScreenPoint(downRightEdge);
             Debug.Log (downRightEdgeScreen);
             if(downRightEdgeScreen.x < 0){
                 Vector3 downRightEdgeScreenFixed = Camera.main.ScreenToWorldPoint(new Vector3(0f, downRightEdgeScreen.y, downRightEdgeScreen.z));
                 float dis = downRightEdgeScreen.x-downRightEdgeScreenFixed.x;
                 Debug.Log("Fixed: "+dis);
             }

Where then dis gives the units i need to move that vector to be inside the screen again. Now how can i convert this dis back to the camera and move the camera to make sure that that vector is inside the screen? So basically what i want to know is, how can i convert units on the x axis at a certain distance to the units the camera has to move cover that distance. Maybe my whole concept is wrong and there are easier ways of doing this, i would be very interested to learn.

Many thanks

Comment
Add comment · Show 1
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 Erik-Sombroek · Jul 22, 2015 at 02:45 PM 0
Share

Hi SaraCecilia thanks for your answer! Clamp would work if it was not for the scaling and the perspective camera.. This makes it hard since i don't have the formule to change the bounds based on the distance of the camera.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Erik-Sombroek · Jul 22, 2015 at 04:15 PM

Hi,

In the end i managed to solve it using a virtual Plane infront of the camera that i raycast onto to get the position of where the camera should be to not have the map out of bounds.

Attached image illustrates the concept: alt text

  • A: We first translate the map bound A to the screen space

  • B: We thenpush point A to the border of the camera (at the Z depth of point A)

  • C: Now we know how much we have to push this vector on the X-axis at given distance so that the Camera is in bounds again.

  • D: Here we translate the center of the camera to the world coordinates at distance Z (distance of the A vector).

  • E: We then offset point D with the distance we found at C

  • F: Now we raycast from point E with a negative direction of camera.forward on to a plane that is placed at the position of the camera with normal facing to camera.forward

Now we know exactly where to place the camera to make sure the camera doesn't go out of the map bounds.

Here is the code, i tried to comment as much as possible:

 Vector3 topRightEdgeScreen = Camera.main.WorldToScreenPoint(topRightEdge);
             Vector3 downLeftEdgeScreen = Camera.main.WorldToScreenPoint(downLeftEdge);
 
             Debug.Log(downLeftEdgeScreen+"  "+Screen.height);
 
             // Is the camera out of the map bounds?
             if(topRightEdgeScreen.x < Screen.width || topRightEdgeScreen.y < Screen.height || downLeftEdgeScreen.x > 0 || downLeftEdgeScreen.y>0){
                 //smack a big plane at the camera position that covers more than the screen is showing
                 cameraPositionFixPlane = new Plane(Vector3.forward*10, Camera.main.transform.position);
 
                 //move the top right edge back so its inside the screen again
                 Vector3 topRightEdgeScreenFixed = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Max(Screen.width, topRightEdgeScreen.x), Mathf.Max(Screen.height,topRightEdgeScreen.y), topRightEdgeScreen.z));
                 //now we know the offset the camera should move at distance z to fix the top right edge
                 Vector3 topRightOffsetAtDistance = topRightEdgeScreenFixed-topRightEdge;
 
                 //this time for the down left edge
                 Vector3 downLeftEdgeScreenFixed = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Min(0, downLeftEdgeScreen.x), Mathf.Min(0, downLeftEdgeScreen.y), downLeftEdgeScreen.z));
                 //now we know the offset the camera should move at distance z to fix the down left edge
                 Vector3 downLeftOffsetAtDistance = downLeftEdgeScreenFixed-downLeftEdge;
 
                 Debug.Log ("offset: "+downLeftOffsetAtDistance);
 
 
                 //where is the center of the screen translated at given distance
                 Vector3 cameraCenterAtDistance = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2.0f, Screen.height/2.0f, topRightEdge.z));
                 //now lets offset the center of the screen with the offset we found
                 Vector3 cameraCenterAtDistanceFixed = new Vector3(cameraCenterAtDistance.x-topRightOffsetAtDistance.x-downLeftOffsetAtDistance.x, cameraCenterAtDistance.y-topRightOffsetAtDistance.y-downLeftOffsetAtDistance.y, cameraCenterAtDistance.z);
 
                 //here we generate a ray at the camera center at distance pointing back to the camera
                 Ray rayFromFixedDistanceToCameraPlane = new Ray(cameraCenterAtDistanceFixed, -Camera.main.transform.forward);
 
                 //this is where the magic happens, lets raycast back to the plane i smacked infront of the  camera
                 float d;
                 cameraPositionFixPlane.Raycast(rayFromFixedDistanceToCameraPlane, out d);
 
                 //where did the raycast hit the camera plane?
                 Vector3 planeHitPoint = rayFromFixedDistanceToCameraPlane.GetPoint(d);
 
                 //position camera at the hitpoint we found
                 Camera.main.transform.position = new Vector3(planeHitPoint.x, planeHitPoint.y, Camera.main.transform.position.z);
 
             }

Hope this will help someone!

Best,

Erik Sombroek


camera-boundaries.jpg (140.4 kB)
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera/can't turn on X 1 Answer

Camera Follow a Target, but not Centered on it 1 Answer

Camera follow players z axis 0 Answers

move a child object to a specific xyz coordinates 2 Answers

A smooth camera follow that allows for rotation functionality independent of the player. Maths? 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