- Home /
The question is answered, right answer was accepted
ForceMode2D.Force not working in fixed update
i am making a 2d rocket type game and i am trying to use rigidbody2d.AddForce with forcemode2d to propel the object
public Rigidbody2D rig;
public float jumpHeight; // set in inspector to 5f
public bool applyForce;
// Update is called once per frame
//checks for input if you are holding down on 'w'
void Update ()
{
if (Input.GetKey("w"))
{
applyForce = true;
}
else
{
applyForce = false;
}
}
private void FixedUpdate ()
{
Ignition();
}
// the function which calls on addforce
public void Ignition ()
{
if (applyForce)
rig.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Force);
}
i tried by calling "Ignition()" in Update and it worked fine, and i tried it in fixed Update and nothing happens, no errors or movement. i tried to run ForceMode2D.Impulse instead of ForceMode2D.Force in fixed update and that worked but i need to use it as a force not impulse anyone know how?
unrelated to the question but if you also know how to change a rigidbodies mass in the script that would help me. also if you know how to change gravities value in physics that would help. Thanks in advance!
Answer by xxmariofer · Feb 11, 2021 at 02:22 PM
If you have a high end pc or your game uses little resources, you could be running the game at a really high frame rate, so at the end be applying much higher forces in Update than in fixedupdate, try increasing the forces value
Also just in case you don't know, Time.deltaTime makes values in update the same no matter what your frame rate is.
@ShuaG I think Flaymes meant to multiply your force by Time.deltaTime, so that it is consistent across all frame rates. You might have to increase your force a bit more as well. For example:
rig.AddForce(new Vector2(0, jumpHeight) * Time.deltaTime, Force$$anonymous$$ode2D.Force);
Hi. If you found the correct answer, please mark you question as Closed(from the edit button). Thank you.