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! :slight_smile:

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.

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 :slight_smile: