Search Unity

Orthographic size for 2D sprites

Discussion in '2D' started by MrXBob, May 17, 2015.

  1. MrXBob

    MrXBob

    Joined:
    May 17, 2015
    Posts:
    2
    I've read that the best way to keep a 1:1 sprite ratio for pixel-perfect art is to "set an orthographic size that matches the vertical screen resolution divided by the pixel height of a sprite" and then to halve that number to set as the orthographic size.

    For testing purposes I'm using my current monitor which is 1680x1050 and my sprites are 80x80, so (1050 / 80) / 2 = 6.5625 right?

    But when I set my orthographic size to 6.5625 this and test it full screen, the sprites are always smaller than they should be.

    What am I doing wrong? Source for the information: http://www.third-helix.com/2012/02/05/making-2d-games-with-unity.html
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    I'm not sure if this is what you're looking for, but I've been using this script (from a tutorial on YouTube) and it seems to do the job.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class ScreenController : MonoBehaviour
    5. {
    6.     public int TargetWidth = 1440;
    7.     public int PixelsToUnits = 32;
    8.  
    9.     private Camera owner;
    10.  
    11.     void Start()
    12.     {
    13.         owner = GetComponent<Camera>();
    14.     }
    15.     void Update()
    16.     {
    17.         int height = Mathf.RoundToInt(TargetWidth / (float)Screen.width * Screen.height);
    18.         owner.orthographicSize = height / PixelsToUnits / 2;
    19.     }
    20. }
     
    theANMATOR2b likes this.
  3. MrXBob

    MrXBob

    Joined:
    May 17, 2015
    Posts:
    2
    I applied that script to the camera but nothing happens (I played around with the numbers and noticed no changes) and despite there being no errors, I get this message below the script in Unity:

    The associated script can not be loaded. Please fix any compile errors and assign a valid script.

    EDIT:

    I'm not sure why it wasn't working before, but trying your code and then reverting back to the code I had before has somehow fixed it! Works great now, this is what I'm using:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.  
    8.     Camera mycam;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.         mycam = GetComponent<Camera> ();
    15.  
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.  
    21.         mycam.orthographicSize = (Screen.height / 100f) / 2f;
    22.    
    23.     }
    24. }
     
    Last edited: May 18, 2015
  4. ColossalPaul

    ColossalPaul

    Unity Technologies

    Joined:
    May 1, 2013
    Posts:
    174
    Did you set your sprite to 80 PixelsPerUnit? That should fix it!