简体   繁体   English

Unity5中的摄像头单元

[英]Camera units in Unity5

i'm currently programming an 2D topview unity game. 我目前正在编写2D顶视图统一游戏。 And i want to set the camera such as, that just a specific area is visible. 我想将摄像机设置为仅可见特定区域。 That means that i know the size of my area and when the camera, which is following the player currently reaches the border of the area, i want to visible stops. 这意味着我知道我所在区域的大小,并且当播放器之后的摄像头当前到达该区域的边界时,我想看到停止位置。

So here is my question: i know where the camera is and how it can follow the player but i dont know how i can calculate the distance between the border of the field and the border of waht the camera sees. 所以这是我的问题:我知道相机在哪里以及它如何跟随玩家,但我不知道如何计算场边界与摄像机看到的边界之间的距离。 how can i do that? 我怎样才能做到这一点?

Essentially, treat your playable area as a rectangle. 本质上,将您的可玩区域视为矩形。 Then, make a smaller rectangle within that rectangle that accounts for the camera's orthographic size. 然后,在该矩形内制作一个较小的矩形,以说明相机的正投影大小。 Don't forget to include the aspect ratio of your camera when calculating horizontal bounds. 计算水平边界时,请不要忘记包括相机的纵横比

Rect myArea;              // this stores the bounds of your playable area
Camera cam;               // this is your orthographic camera, probably Camera.main
GameObject playerObject;  // this is your player

float newX = Mathf.Clamp(
   playerObject.transform.position.x,
   myArea.xMin + cam.orthographicSize * cam.aspect,
   myArea.xMax - cam.orthographicSize * cam.aspect
);
float newY = Mathf.Clamp(
   playerObject.transform.position.y,
   myArea.yMin + cam.orthographicSize,
   myArea.yMax - cam.orthographicSize
);

cam.transform.position = new Vector3(newX,newY,cam.transform.position.z);

If you're using an alternative plane (say xz instead of xy ), just swap out the corresponding dimensions in all the calculations. 如果使用替代平面(例如xz而不是xy ),则只需在所有计算中交换出相应的尺寸即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM