Why the AddForce fonction works differently on different mobiles ?
I have a basketball throwing game and each mobile i test it on, it acts differently. I am using a fonction that i wrote to make the ball jump by adding a force to it's ridgidbody without using the Time.deltaTime or Time.FixedDeltaTime and i am calling that fonction within the FixedUpdate fonction, yet this problem happens...any ideas ? p.s: i tried it with the impulse force mode and without.
public void jump(Vector3 Direction)
{
//adds force to rigidbody
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
Vector2 direction = new Vector2((Direction.x) / 10, 100).normalized;
//force is a pre-determened variable that i use for diiferent kind of balls
GetComponent<Rigidbody2D>().AddForce(direction * force, ForceMode2D.Impulse);
}
Acts differently? Different strength? Fails to do anything sometimes?
Are you calling this function continuously or once? Are you using On$$anonymous$$ey() or On$$anonymous$$eyDown() to trigger it or something else?
Different strength, sometimes acts up to an insane level. Calling it once per click (on mobile). I used a temporary solution which is to calculate the amount of force suitable on that perticular phone starting from a pre-deter$$anonymous$$ed force, and then using the distance the ball went for using the pre-deter$$anonymous$$ed force, to calculate the new more suitable force to apply (calibration).
On this forum at least, about 90 percent of these kinds of problems are caused by handling the (key)input in a way that causes the AddForce call to happen on a few consecutive Update/FixedUpdate calls depending on framerate or other factors. You should show some code related to how this method is called and explain what exactly "works differently" means.
this is the part where the fonction is called, works differently means the object im adding the force to gets shot further in some devices then others. In other words, the outcome of force added isnt the same although the amount of the force it-self is the same.
if (Input.Get$$anonymous$$ouseButtonDown(0)) { t = Input.mousePosition; } if (Input.Get$$anonymous$$ouseButtonUp(0)) { t2 = Input.mousePosition; if (!waiting && Vector2.Distance(t2, t) > width * 0.5f && !clicked && Vector2.Distance(t, gameObject.transform.position) <= width && t2.y > t.y) { jump(t2 - t); } }
Input.mousePosition returns the touch position in pixels. So 1cm drag on a high resolution (dpi actually) device adds more force than a 1cm drag on a low resolution device.
Depending on what you actually want, you should probably use for example Camera.screentoWorldPoint to convert pixel positions to world coordinates, so dragging the same distance in proportion to your game objects causes a proportionate force or Screen.dpi so that pixelsize doesn't affect the force.
Also if the code you show is located in FixedUpdate, depending on framerate, the force might be added multiple or zero times with one drag.
Answer by AquaBomber · Mar 16, 2017 at 08:37 PM
It probably adds different forces because you are adding force bases on the fps rate.To fix it you have to multiply your force by the deltaTime(frames in one second / 1) so : GetComponent().AddForce(direction force Time.deltaTime, ForceMode2D.Impulse);
I already tried your solution and it didn't really help, i even tried to set the fps to 30, or using the VSyncCount and setting it to 2 to slow down the rate. NO change.
Your answer
Follow this Question
Related Questions
Is Collider 2D and OnMouseDown a good combination for mobile? 0 Answers
How to replace space keyboard with a simple tap on Android? 0 Answers
Is it possible to access the native step-counter on mobile platforms. 2 Answers
Is It Bad to be Tired of my Own Mobile Game? 1 Answer
Buttons for Player movement 0 Answers