Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by z7x9r0 · Sep 23, 2015 at 12:13 PM · camerajavascriptpixel art

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image z7x9r0 · Sep 23, 2015 at 07:02 AM 0
Share

I have a feeling I have to use $$anonymous$$athf.Abs somewhere..

avatar image z7x9r0 · Sep 23, 2015 at 07:59 AM 0
Share

$$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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges