# (Beguinner) Inconsistent rb2d.AddForce - multiple calls?

Hello everyone!

So I am making my first game (Android), and I thought things were going pretty well until I stumbled upon something that makes me question my understanding of Unity Physics.

The situation:
My character is “glued” to a wall (with rb2d.constraints.FreezePosition). He will jump in the opposite direction either after a fixed time or if the player taps on it.

The code (C#):

void OnCollisionStay2D(Collision2D other)
{
// Checks if there is a touch close to my character
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
touch = Input.GetTouch(0);
touchposition = Camera.main.ScreenToWorldPoint(touch.position);
if ((touchposition.x > transform.position.x - transform.lossyScale.x) & (touchposition.x < transform.position.x + transform.lossyScale.x) & (touchposition.y > transform.position.y - transform.lossyScale.y) & (touchposition.y < transform.position.y + transform.lossyScale.y))
{
isTouched = true;
}
}

// Makes the character jump if he is touched or after 3 seconds

if (countdown>=3.0f | isTouched)

{
rb2d.constraints = RigidbodyConstraints2D.FreezeRotation; //Removes FreezePosition from the constraints
rb2d.AddForce(-(rb2d.transform.right * 10 * jumplength) + (rb2d.transform.up * 10 * thrust));
IsTouched = false;
}
}

(coutdown is incremented by Time.deltaTime in Update and reset when necessary)

The problem:
The “tap triggered” jump is about half as powerfull as the automatic jump, even though they should be the same. They are both consistent, both vary proportionnaly if I change jumplength or thrust, but I don’t understand why one is weaker than the other. My guess is that the automatic jump is called twice while the tap triggered one is called once, but I can’t figure out why since they are both in the same “if”, in OnCollisionStay2D. (I’ve tried using Forcemode, no change).

Any Idea what is happening?

What if you reset the countdown when you jump?

``````if (countdown>=3.0f || isTouched) //I prefer || rather than just |, because if the countdown is >=3.0f you don't need to check for isTouched

{
rb2d.constraints = RigidbodyConstraints2D.FreezeRotation; //Removes FreezePosition from the constraints
rb2d.AddForce(-(rb2d.transform.right * 10 * jumplength) + (rb2d.transform.up * 10 * thrust));
IsTouched = false;

countdown = 0f;// Set back the CD to zero. I guess you need to make the CD to stop incrementing as well.

}
``````

Maybe it’s because OnCollisionStay is called by fixedUpdate, not by Update. Sometimes, fixedUpdate happends twice during the same Update loop.

It works! My god it was so simple…
Thank you kind hearted stranger, you avoided me countless headaches!