Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Keeping Player Inside Bounds of a Moving Camera

Discussion in '2D' started by IrocJeff, Jan 23, 2020.

  1. IrocJeff

    IrocJeff

    Joined:
    Sep 13, 2019
    Posts:
    34
    My first unity project is going to be a game similar to Lifeforce or R-Type so that will give a example of what I want to accomplish.

    I have a camera that moves at a steady velocity to the right and my player is a child of the camera. This so far is working well. The problem comes with bounding the player to the camera. If I give it bounds i'm going to hit a wall and my player will stop which i did in my test. However, in the games I mentioned you are always bound to the screen. You can't go off the screen as you are blocked. The top and bottom bounds are not the issue its more the right side and left side bounds.

    This is what I have in my test setup now

    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.position = new Vector3(Mathf.Clamp(transform.position.x, -5f, 5f),
    4.         Mathf.Clamp(transform.position.y, -3f, 3f), transform.position.z);
    5.     }
    It seems I'd have to constantly update my min and max X positions but I haven't the foggiest clue on how.

    Would it be feasible to create a non visible barrier at the end of both X positions that moves along the same speed as the camera and blocks the player instead?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    I use this utility I made to get the Left, Right, Top and Bottom of the camera. It also has a function to constrain the camera to the specified Bounds.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class ScreenUtility
    4. {
    5.     public static Camera camera;
    6.  
    7.     public static float Left
    8.     {
    9.         get
    10.         {
    11.             if (camera)
    12.                 return camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).x;
    13.  
    14.             if (Camera.main)
    15.                 return Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).x;
    16.  
    17.             return 0.0f;
    18.         }
    19.     }
    20.  
    21.     public static float Right
    22.     {
    23.         get
    24.         {
    25.             if (camera)
    26.                 return camera.ViewportToWorldPoint(new Vector3(1.0f, 0f, 0f)).x;
    27.  
    28.             if (Camera.main)
    29.                 return Camera.main.ViewportToWorldPoint(new Vector3(1.0f, 0f, 0f)).x;
    30.  
    31.             return 0.0f;
    32.         }
    33.     }
    34.  
    35.     public static float Top
    36.     {
    37.         get
    38.         {
    39.             if (camera)
    40.                 return camera.ViewportToWorldPoint(new Vector3(0f, 1.0f, 0f)).y;
    41.  
    42.             if (Camera.main)
    43.                 return Camera.main.ViewportToWorldPoint(new Vector3(0f, 1.0f, 0f)).y;
    44.  
    45.             return 0.0f;
    46.         }
    47.     }
    48.  
    49.     public static float Bottom
    50.     {
    51.         get
    52.         {
    53.             if (camera)
    54.                 return camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).y;
    55.  
    56.             if (Camera.main)
    57.                 return Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).y;
    58.  
    59.             return 0.0f;
    60.         }
    61.     }
    62.  
    63.     public static Vector3 Center
    64.     {
    65.         get
    66.         {
    67.             if (camera)
    68.                 return camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
    69.  
    70.             if (Camera.main)
    71.                 return Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
    72.  
    73.             return Vector3.zero;
    74.         }
    75.     }
    76.  
    77.     public static bool ScreenContainsPoint(Vector3 worldPosition)
    78.     {
    79.         return Camera.main.rect.Contains(Camera.main.WorldToViewportPoint(worldPosition));
    80.     }
    81.  
    82.     public static void ConstrainCamera(Camera camera, Bounds bounds)
    83.     {
    84.         float left = camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).x;
    85.         float right = camera.ViewportToWorldPoint(new Vector3(1.0f, 0f, 0f)).x;
    86.         float top = camera.ViewportToWorldPoint(new Vector3(0f, 1.0f, 0f)).y;
    87.         float bottom = camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).y;
    88.  
    89.         if (top > bounds.max.y)
    90.         {
    91.             float topDiff = bounds.max.y - top;
    92.             camera.transform.position += new Vector3(0, topDiff, 0);
    93.         }
    94.         else if (bottom < bounds.min.y)
    95.         {
    96.             float botDiff = bounds.min.y - bottom;
    97.             camera.transform.position += new Vector3(0, botDiff, 0);
    98.         }
    99.  
    100.         if (right > bounds.max.x)
    101.         {
    102.             float rightDiff = bounds.max.x - right;
    103.             camera.transform.position += new Vector3(rightDiff, 0, 0);
    104.         }
    105.         else if (left < bounds.min.x)
    106.         {
    107.             float leftDiff = bounds.min.x - left;
    108.             camera.transform.position += new Vector3(leftDiff, 0, 0);
    109.         }
    110.     }
    111. }