- Home /
Maintain equal physics over different UI sizes?
Hello, quick question.
I am making a fishing game for Android similar to how fishing works in Stardew Valley. There is an area (UI Image) which you need to keep over a moving fish (up and down). Down is set by rigidbody2d gravity on the UI image, up by add force on the UI image. Gravity is calculated through selected gear, upward force through subtracting the "speed" stat of selected gear from the gravity. Canvas is set to screenspace camera and to scale with screen size.
The problem: Gravity/AddForce act differently based on screen size. Maybe due to the UI getting bigger the bigger the screen is, thus requiring more force to elevate and gravity to drop?
I thought to maybe multiply the UI image rigidbody2d with a number calculated from screen height which gets bigger the higher (read: larger) the screen is to compensate this. How could I do that? Are there other possibilities?
Many thanks! :)
Answer by merkaba48 · Jul 04, 2017 at 07:16 PM
No experience using physics on UI elements...but check that the mass isn't automatically generated (it's a flag in the RigidBody2D inspector). If it is, then Unity will increase the mass of the object the larger it is.
Hi, thanks for your answer. No, the mass isn't automatically generated. I think the reason that it behaves differently on different screen sizes is due to the fact that the UI scales with the screen. The larger the screen, the larger the UI - which means it has to travel greater distances in equal time to behave the same as on a smaller scale.
I'll put some research into it tomorrow and compare the sizes and forces acting onto it in realtime through the Logger. $$anonymous$$aybe I'll find an answer there.
Answer by LimaoMatador · Jul 04, 2017 at 10:23 PM
I've never tried to add Rigidbodies to UI elements either, but you'll probably have some problems, since rigidbodies use absolute 2D world coordinates and the UI element uses screen space coordinates that are relative to the UI element anchor, so you're probably right about the UI scaling changing your object behaviour.
I advise you to create a 2D GameObject with the Rigidbody2D in your game world, without any sprite or graphics. It's just a reference object that will be driven by your game rules. Then, create a separate UI element that follows that object. To do that, you'll need to convert the object's world position to UI screen space.
If you don't know how to do that, this thread has some solutions: How to convert from world space to canvas space?
I guess this setup is more reliable, hope it helps :)
Your answer
Follow this Question
Related Questions
Jitter/vibration on an object with constant velocity when using 2D Pixel Perfect Camera 1 Answer
How to improve device heating (Android) 1 Answer
How do you detect if two specific objects are touching each other without collision. 3 Answers
Physics2D Overlapbox not working as aspected 0 Answers
Smooth transition for RotateAround 1 Answer