- Home /
Rigidbody2D velocity relative to screen size
Hey all I am having a problem with velocities. When I view the velocity in small resolution, it appears fast but when I see it in full screen, it appears slower. I know that it just appears to be so but I don't even want it to appear so. Any ideas how to make it appear same in all resolutions? Please Help!
Answer by Tony-Tonijn · Jul 18, 2017 at 12:39 PM
Depending on what camera you're using (I've assumed orthographic as you are using 2D rigidbodies) you would just use a scalar and the orthographic size of the camera to determine the increase in speed.
rigidbody2D.velocity *= someCamera.orthographicSize * someScalar;
However this might become a lot more complex if you are using a perspective camera. In that case you need to account for both the size of the FOV and the distance to the object. Here you would probably want to measure the bounding box of the object you want to display against the size of the screen in pixels. You can find the later two in Screen.width and Screen.height.
I hope this answers your question somewhat.
Can you please explain what is a scalar? I am a newbie in unity so i don't know many terms sorry.
Orthographic size of the camera is 5. Can you please help me with a few lines of code for accomplishing what you are saying?
Scalar just means a number basically.
What is is it that you actually want?
For the object to move the same amount of pixels/second or same amount of real world millimeters/second or something else?
Depending on the answer you'll have to know at least the resolution and maybe the dpi or physical size of the screen.
Then depending on whether you want just the speeds or also object sizes to scale, use the scalar to resize the camera and/or the game objects. If you change object sizes, they'll move at different speed in relation to their size on different sized screens.
The problem seems kind of weird and there shouldn't be a reason for having to do this 99% of time so it might be useful to try to describe the root of the problem (for example from player's point of view) and wanted end result a bit more.
I want the object to become larger and the speed to basically increase by the same amount the screen size is increasing (keep the velocity constant relative to the screen size) so as to make it appear same in all screen sizes.
Please post comments as comments, not answers. I converted this one.
Unity already does things like this. If you use an orthographic camera with orthographic size of 5 for example, it means the visible game area on any screen is 2x5 units high from bottom of screen to top of screen. Also a sphere with a 1 unit diameter will be 1/10 of the screen height on any device.
The physics engine uses those same game world units to do its calculations so for example an object moving 1 units per second upwards will take 10 seconds to travel from bottom of screen to top of screen on any device if the game's orthographic camera has a size of 5.
The aspect ratio of the device deter$$anonymous$$es how many game world units are visible width-wise (orthographic camera). Similar rules apply when using perspective camera.
If you scale objects, it will change how the game plays because they will now collide more often or less often. If you change camera settings, it will change gameplay because it changes when objects enter and exit the screen
Your answer
Follow this Question
Related Questions
Moving game object have different speeds in different screen resolutions and it's jittering 1 Answer
Scaling an object causes slow movement (Since Update 5.5.4) 1 Answer
,Rotate a gameobject around another while being attracted by its gravity 1 Answer
Modifying a kinematic rigidbody2D.velocity makes player to go through the wall 0 Answers
rigidbody2D maximum y velocity 1 Answer