Search Unity

  1. Unity 2018.3 is now released.
    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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    415
    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?