• 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 /
avatar image
Question by PuneetK · Oct 19, 2014 at 03:33 PM · physicsvelocityorthographic

Adjusting Camera.OrthographicSize is messing up game Physics?

Hi guys, I am trying to build a 2 player turn based game using Unity 2D. It is very necessary for me that the game run exactly the same on all screen resolutions as it is completely Physics Based and is a limited field game somewhat similar to Ice hockey.

Here is a simple gameplay video of my game built for PC resolution 1280x720 http://youtu.be/HmZ7RH7ul_U

Now, I want the game to look exactly the same on all resolutions, or atleast, be played exactly the same.

Currently with no alterations at all, here is what happens alt text

Now, what I actually want to happen in the 3:4 resolution is something like this alt text

Now, to achieve that, I have found a way. I am adjusting my Camera's orthographic size based on the current screen height.

This method does have it's flaws and I am working a way to fix those flaws out. Here is the script I am using to achieve it:

 void Start () 
     {
         float screenHeight = Screen.height;
 
         float screenWidth = Screen.width;
         
         float myWidth = renderer.bounds.size.x;
         
         Vector3 myScale = transform.localScale;
         
         float numerator;
         float denominator;
         float reqScale;
         
         //For Width Calculation
         numerator = myScale.x * reqWidth;
         denominator = myWidth/screenWidth;
         reqScale = numerator / denominator;
         myScale.x = reqScale;
         myScale.y = reqScale;
 
         transform.localScale = myScale;    
     }

Now, As I said before, this is scaling my game along with X axis (Width) in a way that is working out great for me, but the problem here now is that my ingame physics is getting messed up.

At the start of the game, I give the ball a velocity of magnitude 5, in the Up direction

 void ReleaseBall(Vector3 ballPos, Vector2 ballVelocity)
 {
 //Here ballVelocity is Vector2(0,5f)
    
         transform.position = ballPos;
         rigidbody2D.velocity = ballVelocity;
 }

Depending on the new Camera, the ball may take a varying time to reach one of the gravity fields.

Likewise, once inside the gravity field I perform the following calculation:

 if(!turnOffForce)
             {
                 rad = (other.transform.position - this.transform.position);
                 int myDot = (int)Vector2.Dot(rad, other.rigidbody2D.velocity);
                 print ("Value of Rad: " + rad);
                 print ("Value of myDot: " + myDot);
                 if(myDot == 0 && !trigger)
                 {
                     trigger = true;
                     vel = other.rigidbody2D.velocity;//.magnitude;
                     dist = Vector2.Distance(other.transform.position, this.transform.position);                            
                     centriMagnitude = ((m1 * Mathf.Pow(vel.magnitude, 2)) / dist);
                     //doOnce = false;
                 }
                 if(trigger == true)
                 {
                     centripetal = rad/rad.magnitude;    
                     other.rigidbody2D.AddForce(centripetal * -centriMagnitude);
                 }    
             }

In the above script, the value for myDot never comes to 0 anymore. Depending on the resolutions, the minimum values are -69. -211, etc. but never 0.

On hardcoding if(myDot == -211) I get messed up rotation physics. The ball goes crazy and oscillates around the center at insane speeds, and ultimately shoots itself out of the field.

I am sorry for the long post. My question in short: How does changing the orthographic size of my camera affect the inbuilt physics system? And how can I normalize the physics for each and every resolution, assuming my camera's size will change depending on the res

Sidenote: I feel this method of aspect ratio handling may be incorrect, so if there is a better way to achieve the same or similar result, please do let me know.

untitled.png (60.7 kB)
untitled2.png (28.0 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by deckerjeffreyr · Nov 16, 2015 at 08:42 AM

I know this is a little dated but did you ever find an answer for this?

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

How to move a gameObj by cicking/ touching another gameobj? 0 Answers

How to transfer bat's transnational and angular velocity to ball to simulate realistic ball movement. 0 Answers

Player slows down when jumping/velocity changes 1 Answer

Set Velocity of Object According to Rotation 2 Answers

How to Smooth Out Constant Velocity on Rigidbody2D 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