Search Unity

Resolution trouble

Discussion in '2D' started by DirtyMeat, Oct 10, 2015.

  1. DirtyMeat

    DirtyMeat

    Joined:
    Oct 21, 2014
    Posts:
    3
    Ok so im having trouble getting the camera to go all the way to the edge of the screen when i run the game, works fine in Free Aspect on the Game view, but as soon as i Maximize on Play the camera wont move beyond the bounds. I feel like my issue is where i get the values for the bounds of the rendered background. Can someone help me make it so that i can get proper values for each aspect ratio, so that i may play this on more than one ratio. I put in the code for my Following Camera

    **Update** So if i start in a new location and travel to the Home screen, the camera works fine..Guys this is killing me, i cant figure this out.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FollowCamera : MonoBehaviour {
    4.  
    5.     // Distance in the x axis the player can move before
    6.     // the camera follows.
    7.     public float xMargin = 1.5f;
    8.  
    9.     // Distance in the y axis the player can move before
    10.     // the camera follows.
    11.     public float yMargin = 1.5f;
    12.  
    13.     // How smoothly the camera catches up with its target
    14.     // movement in the x axis.
    15.     public float xSmooth = 1.5f;
    16.  
    17.     // How smoothly the camera catches up with its target
    18.     // movement in the y axis.
    19.     public float ySmooth = 1.5f;
    20.  
    21.     // The maximum x and y coordinates the camera can have.
    22.     private Vector2 maxXAndY;
    23.  
    24.     // The minimum x and y coordinates the camera can have.
    25.     private Vector2 minXAndY;
    26.  
    27.     // Reference to the players transform.
    28.     public Transform player;
    29.  
    30.     void Awake() {
    31.  
    32.  
    33.         // Setting up the reference.
    34.         player = GameObject.Find("Player").transform;
    35.         if (player == null) {
    36.             Debug.LogError("Player object not found");
    37.         }
    38.  
    39.         // Get the bounds for the background texture - world size
    40.         var backgroundBounds = GameObject.Find("background").GetComponent<SpriteRenderer>().bounds;
    41.  
    42.         // Get the viewable bounds of the camera in world
    43.         // coordinates
    44.         var camTopLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0));
    45.         var camBottomRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 0));
    46.  
    47.         // Automatically set the min and max values
    48.         minXAndY.x = backgroundBounds.min.x - camTopLeft.x;
    49.         maxXAndY.x = backgroundBounds.max.x - camBottomRight.x;
    50.  
    51.     }
    52.  
    53.     bool CheckXMargin() {
    54.         // Returns true if the distance between the camera and the
    55.         // player in the x axis is greater than the x margin.
    56.         return Mathf.Abs(this.transform.position.x - player.position.x) > xMargin;
    57.  
    58.     }
    59.  
    60.     bool CheckYMargin() {
    61.         // Returns true if the distance between the camera and the
    62.         // player in the y axis is greater than the y margin.
    63.         return Mathf.Abs(this.transform.position.y - player.position.y) > yMargin;
    64.     }
    65.  
    66.     void FixedUpdate() {
    67.         // By default the target x and y coordinates of the camera
    68.         // are it's current x and y coordinates.
    69.         float targetX = transform.position.x;
    70.         float targetY = transform.position.y;
    71.  
    72.         // If the player has moved beyond the x margin...
    73.         if (CheckXMargin()) {
    74.             // the target x coordinate should be a Lerp between
    75.             // the camera's current x position and the player's
    76.             // current x position.
    77.             targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.fixedDeltaTime);
    78.         }
    79.  
    80.         // If the player has moved beyond the y margin...
    81.         if (CheckYMargin()) {
    82.             // the target y coordinate should be a Lerp between
    83.             // the camera's current y position and the player's
    84.             // current y position.
    85.             targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.fixedDeltaTime);
    86.         }
    87.  
    88.         // The target x and y coordinates should not be larger
    89.         // than the maximum or smaller than the minimum.
    90.         targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
    91.         targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
    92.  
    93.         // Set the camera's position to the target position with
    94.         // the same z component.
    95.         transform.position = new Vector3(targetX, targetY, transform.position.z);
    96.     }
    97. }
    98.  
     
    Last edited: Oct 10, 2015
  2. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    436
    Try putting the bound calculation within your FixedUpdate i.e. calculated every time.