How to move 2D Object within camera view boundary

后端 未结 4 1623
感动是毒
感动是毒 2020-12-21 23:47

I have a scene that my camera doesn\'t follow my player. When player reaches the end of camera I want player to can\'t go further (out of camera view). How can I do this?

4条回答
  •  执念已碎
    2020-12-22 00:22

    Possible solution is:

    1.Get the coordinates of your screen cornes (top left, top right, bottom left, bottom right). You can get this coordinates using Screen.height and Screen.width.

    2.Convert this coordinates using the camera you need with Camera.ScreenToWorldPoint.

    3.Get your player coordinates and check that they are inside the rect which is formed by 4 coordinates of the screen corners.

提交回复
热议问题