Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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. Cucci_A

    Cucci_A

    Joined:
    Jan 23, 2018
    Posts:
    413
    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?