- Home /
How to standardize player input for AddForce on mouse/finger cursor
Hello! I'm trying desperately to figure out how to standardize the amount of force we add to objects in our game.
The game works by an invisible cursor (controlled by the mouse or your finger) that pushes dots around toward goals. Every frame that the dot is within range of the cursor force is added to the dot depending on the delta (velocity) of the cursor's movement.
The cursor's "velocity" is clamped x/y between -1f and +1f. So it is basically normalized (we used clamp because we were loosing small movements when using normalize). The force added is multiplied by (Time.smoothDeltaTime * 60f) to normalize for frame rate.
The issue is that I can't figure out what the difference is on each device that is requiring us to multiply the force added.
On Nexus 7 and Nexus 5 we're multiplying the force only by 80,000 In the Unity Editor we need to multiply the force by 500,000 In a Windows standalone build the force multiplier needs to be about 120,000
I have tried adjusting for frame rate - no change. I have tried to see the correlation between DPI or actual screen diagonal in inches and it doesn't correlate. For example:
The Nexus 7 is 7" with DPI of 216, 30FPS and requires a multiplier of 80,000
The Nexus 5 is 4.95" with DPI of 445, 60FPS and multiplier is also 80,000
The Unity Editor window size is 10.5", DPI 96, around 90FPS needs 500,000 multiplier
Unity Windows standalone window size is 8.5", DPI also 96, around 90FPS needs 120,000 multilpier
The code is basically like this: FixedUpdate() determine change in mouse position since last frame, clamp to -1,1 -> this is used as velocity
OnTriggerEnter2D/OnTriggerStay2D() addForce impulse to each dot depending on it's distance from the mouse point with the following calculation:
Vector2 force = mouseVel * forceMulti * ratio * 1f / (Time.deltaTime * 60f);
ratio = distance from center
mouseVel = mouse velocity as explained above
forceMulti = the multiplier I'm trying to figure out how to standardize
1f / (Time.deltaTime * 60f) = my attempt to adjust for framerate because it seems that the higher the framerate the more force that needs to be added.
So... I have been trying all sorts of different things with little to no success. I'm now trying to determine the force multiplier in relation to the physical screen size based on the DPI and the screen's diagonal, but the relationship seems to be exponential which doesn't make sense.
I posted code before, but no one would help. I'll post code again if necessary, but I feel this can be discussed without it and my code is currently a mess from trying so many different things.
Please help! Thanks!!
EDIT: A short video of the game can be seen here: http://naplandgames.com/images/forum_posts/gravitone_short.avi
Thanks. That's one of the first things I tried. The issue is not framerate dependent. I've ruled that out with fixing the framerate with Application.targetFrameRate and setting vSync skip to 2. Both yielded a fairly solid 30FPS and still on Windows the editor is much more sensitive than an actual build. I've also now rewritten everything to exclude as much outside influence as possible. I use my own calculations for detecting distance and "collision" ins$$anonymous$$d of Unity's collision events, everything is now in FixedUpdate and I'm using impulse mode on the force added to the objects. Still wonky in windows, but it seems to be much better on mobile. Very confusing.
hello @Naphier, I have one idea about your problem. You use your code inside FixedUpdate, so you use time.deltaTime.
Just put your code inside InvokeRepeating. and call invoke repeating at 0.03 sec(30 FPS). and don't use time.deltaTime. So it approx same time for every platform.
NOTE :- I think it's not best way to do this. And it might be not good for performance also. but Just try it out and tell me is it worked?
I don't think InvokeRepeating is a good idea as it needs to be high performance and that's going to be a lot of calls to it. But maybe it would be O$$anonymous$$. I'm not planning to try it as it seems pretty good now.
I think that there were just too many factors and the velocity of the mouse/finger was probably the biggest problem. Since a large movement on a 5" device is medium movement on a 7" device.
I'm dealing with integrating a bunch of plugins at the moment and they're breaking other plugins so I am only co$$anonymous$$g back to this if I find issues again.