Simple math, can not figure out why I am getting a different answer.
Developing a mobile game and needed pixel perfect sprites. The tablet I'm using to test with is 1920x1200. I can't figure out why I get a different answer on paper than what the script I made below is returning me.
function Update(){
if(ScreenOrientation.Portrait){
GetComponent.<Camera>().orthographicSize = ((Screen.currentResolution.height)/(3*32))*0.5;
}
if(ScreenOrientation.Landscape){
GetComponent.<Camera>().orthographicSize = ((Screen.currentResolution.height)/(4*32))*0.5;
}
}
I used this JavaScript to debug:
#pragma strict
var text1 : UnityEngine.UI.Text;
var text2 : UnityEngine.UI.Text;
var text3 : UnityEngine.UI.Text;
var mainCamera : Camera;
function Update () {
//Displays Debug Text
text1.text = "Screen Height = " + Screen.currentResolution.height;
text2.text = "Screen orientation = " + Screen.orientation;
text3.text = "Orthographic Size = " + mainCamera.orthographicSize;
}
For Portrait I get the following from the Debug.js
Screen Hieght = 1920
Screen orientation = Port
Orthographic Size = 7.5
When it should be Orthographic Size = 10
And for Landscape I get the following from the Debug.js
Screen Hieght = 1200
Screen orientation = Lan
Orthographic Size = 4.5
When it should be Orthographic Size = 4.6875
I think the Landscape is an easy fix since it seems to be rounding (also, why down???), but I have no clue what is happening when the screen is in Portrait mode. Where did 7.5 come from?
Any help figuring this out would be amazing even though I might just be overlooking it, which is driving me crazy. Thanks.
I have a feeling I have to use $$anonymous$$athf.Abs somewhere..
$$anonymous$$ore testing shows that some how the Portrait $$anonymous$$ode if statement has an effect on the Landscape $$anonymous$$ode if statement. I learned this by changing the value that is multiplied by 3.
Answer by z7x9r0 · Sep 23, 2015 at 06:27 PM
So I combined both @sys12 and @Dave Carlile's advice. This is what I have now. Everything seems to work as they should. All the pixels now line up since my sprites are 32x32. Thank you two for your help. I've been trying to figure out how to get pixel perfect art working for weeks and now seem to have it out of the way. Thank you.
#pragma strict
function Update(){
if(Screen.orientation==ScreenOrientation.Portrait || Screen.orientation==ScreenOrientation.PortraitUpsideDown){
GetComponent.<Camera>().orthographicSize = ((Screen.currentResolution.height)/(3.0*32.0))*0.5;
}
if(Screen.orientation==ScreenOrientation.Landscape || Screen.orientation==ScreenOrientation.LandscapeRight || Screen.orientation==ScreenOrientation.LandscapeLeft){
GetComponent.<Camera>().orthographicSize = ((Screen.currentResolution.height)/(4.0*32.0))*0.5;
}
}
Answer by sys12 · Sep 23, 2015 at 12:19 PM
Ok. Shouldn't your conditions be Screen.orientation==ScreenOrientation.Portrait
and Screen.orientation==ScreenOrientation.Landscape
?
Answer by Dave-Carlile · Sep 23, 2015 at 12:19 PM
First, you're using integer math, so any float point values get truncated off. For your landscape example:
1200 / 128 = 9.375.
The 9.375 gets truncated to 9, 9 * 0.5 = 4.5.
In the portrait example, if you do the landscape math you get the 7.5 value:
1920 / 128 = 15, 15 * 0.5 = 7.5.
Add Debug.Log calls to make sure the value of screen orientation is what you expect.
If you change the 3/32 to 3.0 / 32.0 it should use floating point math for everything (I think). If it doesn't then you can typecast the height to a float.
Your answer
Follow this Question
Related Questions
Show device camera preview (Android & iOS) 1 Answer
help with switching camera 0 Answers
2D renderer not allowing camera stacking with pixel perfect camera 0 Answers
Translate between C# and javascript 1 Answer
Is there a way to smoothly change a float number between two numbers when a key is pressed? 1 Answer