I am new to both Unity 5 and C#, This is the script that was suggested to me. This was even more rough when it was given to me. I have polished it to the best of my ability. Anoyone that can help is greatly appreciated. Thank you for your time and have a great day.
using UnityEngine;
using System.Collections;
public class PixelPerfectCamera : MonoBehaviour {
public static float pixelsToUnits = 1f;
public static float scale = 1f;
public Vector2 nativeResolution = new Vector2 (240, 160);
void Awake () {
var camera = GetComponent<Camera> ();
if (camera.orthographic) {
scale = Screen.height / nativeResolution.y;
pixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f / pixelsToUnits);
}
}
}
If you do some algebra you will see that the calculation should always give 80 as an answer:
pixelsToUnits *= scale → pixelsTounits starts with 1 so pixeslToUnits = scale
Then
camera.orthographicSize = Screen.height / 2.0f / scale
camera.orthographicSize = Screen.height / 2.0f / (Screen.height / nativeResolution.y)
camera.orthographicSize = 1 / 2.0f / ( 1/nativeResolution.y)
camera.orthographicSize = nativeResolution.y / 2.0f
camera.orthographicSize = 160 / 2.0f = 80
The calculation is therefore a bit strange to me and should be reconsidered.
I’m a beginner in C# so I may be wrong but:
I assume it’s your math?
Say the window is (500,500)
scale = 500/160
scale = 3.125
pixelstounits = 3.125 (?)
orthographiSize = 500/2/3.125 = 25
So I can see you getting 9 with what ever resolution you’re using. Just clean up your math to get the desired effect. Pen and paper is super useful!
Hope this helps.