Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AddForce on rigidBody2D not always working

Discussion in 'Physics' started by bribat3592, Nov 6, 2018.

  1. bribat3592

    bribat3592

    Joined:
    Nov 6, 2018
    Posts:
    2
    Hello,

    I am making a 2D game in Unity v.2018.2.14. I am having an issue where the AddForce call for the player's rigidBody2d is not always working. Here is the code:

    // Update is called once per frame
    void FixedUpdate () {
    bool jump = false;
    jump = Input.GetKeyDown(KeyCode.Space);
    if (jump == true)
    {
    animController.CrossFade("Frank-startjump", .2f);
    rigidBody.AddForce(Vector2.up * 3f, ForceMode2D.Impulse);
    jumpStarted = true;
    }
    if((rigidBody.velocity.normalized.y < 0) &&
    (jumpStarted == true))
    {
    jumpStarted = false;
    animController.CrossFade("Frank-endjump", .2f);
    }
    }

    What am I doing wrong?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You're performing all of your logic in FixedUpdate. FixedUpdate should only be used with physics, while other things such as reading inputs and comparing values should be done in Update.
     
  3. bribat3592

    bribat3592

    Joined:
    Nov 6, 2018
    Posts:
    2
    @Cucci_A
    Thanks for your reply. Are you saying that the only thing in FixedUpdate should be the following line:

    rigidBody.AddForce(Vector2.up * 3f, ForceMode2D.Impulse);

    If so, how can I accurately synchronize the start and end jump animations with the force movement of the player?