Better Jumping in Unity: Optimizations

Поділитися
Вставка
  • Опубліковано 17 жов 2024

КОМЕНТАРІ • 42

  • @Valoo24
    @Valoo24 6 років тому +39

    For those who are trying tu use the BetterJump script in 3D, i did this one and it works perfectly well:
    {
    public float fallMultiplier = 3f;
    public float lowJumpMultiplier = 2f;
    public float gravityScale = 1f;
    private float globalGravity = -9.81f;
    private Rigidbody rb;
    void Start ()
    {
    rb = GetComponent();
    rb.useGravity = false;
    }
    void FixedUpdate ()
    {
    Vector3 gravity = globalGravity * gravityScale * Vector3.up;
    if (rb.velocity.y < 0)
    {
    rb.AddForce(gravity * fallMultiplier, ForceMode.Acceleration);
    }
    else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    {
    rb.AddForce(gravity * lowJumpMultiplier, ForceMode.Acceleration);
    }
    else
    {
    rb.AddForce(gravity, ForceMode.Acceleration);
    }
    }
    }

    • @justinsmith26
      @justinsmith26 Рік тому

      4 years later and not sure how many changes have been made to Unity since this was posted
      - however this is not a good solution as it is constantly adding a force to the character whether jumping or not.
      Just thought I would leave this here for Posterities sake.

  • @utkarshjha9547
    @utkarshjha9547 5 років тому +1

    This just saved my life. I could not figure out why my character sometimes did not jump. thank you so much!!!

  • @1e1001
    @1e1001 5 років тому +16

    It's *better* better jumping code

  • @Recfutfull
    @Recfutfull 6 років тому

    I have a question. If i'm using built-in unity physics and with addforce function for jumping my player get additional force by hitting the very angle of the box collider, is this a catching of additional input cause it or i've messed up with code?

    • @1e1001
      @1e1001 5 років тому +2

      You've messed up with Grammer

    • @aquaarmour4924
      @aquaarmour4924 5 років тому

      @@1e1001 *Grammar

    • @1e1001
      @1e1001 4 роки тому

      @@aquaarmour4924 shite

  • @yoctometric
    @yoctometric 6 років тому +1

    Thank you very much for updating this!

  • @Joonysuke
    @Joonysuke 4 роки тому

    Any particular reasons why you would need two(2) scripts for jumping? I'm just curious as to the approach compared to simply making it all work in a single script.

  • @youseffawzi5033
    @youseffawzi5033 4 роки тому

    This channel must be more popular!

  • @justwantawholename
    @justwantawholename 5 років тому +3

    Aren't you still checking input in the fixedUpdate though? Jump key is being checked

    • @lhorbrum1818
      @lhorbrum1818 5 років тому

      I also thought this, could someone explain please?

    • @alejmc
      @alejmc 5 років тому

      He just forgot about it. Same rules apply. Probably a clean way would be to have a “player” class with a IsJumping property, IsOnGround property (or wall, or ceiling, etc). That class is referenced by the BetterJump one.
      The main player class does most on the update, input checking and all. The BetterJump class does all the physics fixed update thingies.

    • @nicolas4677
      @nicolas4677 3 роки тому

      The difference is that he is setting the value of the JumpRequest variable in the Update, not in the FixedUpdate. Usually, you want to get input values in the Update method because it gets called once per frame, in the other hand, the FixedUpdate can get called multiple times per frame.

  • @DsiakMondala
    @DsiakMondala 6 років тому

    If gravity is only normal when the y speed is 0, it makes no difference to set it back?

  • @johnhubbard4038
    @johnhubbard4038 6 років тому +10

    How do you reset the gravity in 3d?

    • @AetherXIV
      @AetherXIV 4 роки тому

      yeah that is my question :)

    • @papathanos1353
      @papathanos1353 3 роки тому

      something like Physics.gravity = new Vector3(0,0,0)

  • @damianrozpedek1049
    @damianrozpedek1049 4 роки тому

    5:00 i just did like this:
    rb.velocity=new Vector2(rb.velocity.x, rb.velocity.y* jumpVelocity)
    So the jump doesn’t interrupt if im moving left or right.

  • @de_soot
    @de_soot 4 роки тому +1

    This saved my life thx.

  • @FengolZA
    @FengolZA 6 років тому +1

    This was really useful! Thank you! Are you going to do a video for moving vertically as well?

    • @BoardToBitsGames
      @BoardToBitsGames  6 років тому

      +André Odendaal I have a series on making a character controller as well that might meet your needs

    • @FengolZA
      @FengolZA 6 років тому

      Does it work with this better jump code?

  • @allwayne8909
    @allwayne8909 4 роки тому

    Thanks, it helped me a lot.
    But I have a question there maybe someone can help me.
    I would like to jump up with more strength. And do not stay in the air for so long with the jump button pressed.
    Maybe someone has an idea how I could do that?

  • @RodrigoMartaFoto
    @RodrigoMartaFoto 6 років тому

    You fixed my issue, thanks a lot!

  • @midend
    @midend 6 років тому +6

    not caching the rigidbody?

    • @BoardToBitsGames
      @BoardToBitsGames  6 років тому +1

      Also good to do, I had just overlooked it. Since it's not being run every frame, it's not the end of the world, but caching is always a good idea.

  • @Dman07ful
    @Dman07ful 4 роки тому

    how to add double jump on that code?

  • @StereoPT
    @StereoPT 6 років тому

    Great Video man.
    Hope I can someday reach the same audience as you.

  • @DerikDF
    @DerikDF 3 роки тому

    It is not useful ... it is necessary to detect the "Ground" before jumping.

  • @ElefanT1396
    @ElefanT1396 4 роки тому

    Please can you make it also for 3D

  • @thatsalot3577
    @thatsalot3577 2 роки тому

    haha, this is how 4 lines become a big fat chunk of code

  • @carasusan
    @carasusan 5 років тому +1

    Allright.. thanks for the Tutorial. But its just working with 2d!! There is no gravityScale for 3D rigidbody. Should be more complex

    • @pavek7193
      @pavek7193 4 роки тому +2

      I don't know if it's too late, but I found this in the comments :D
      {
      public float fallMultiplier = 3f;
      public float lowJumpMultiplier = 2f;
      public float gravityScale = 1f;
      private float globalGravity = -9.81f;
      private Rigidbody rb;
      void Start ()
      {
      rb = GetComponent();
      rb.useGravity = false;
      }
      void FixedUpdate ()
      {
      Vector3 gravity = globalGravity * gravityScale * Vector3.up;
      if (rb.velocity.y < 0)
      {
      rb.AddForce(gravity * fallMultiplier, ForceMode.Acceleration);
      }
      else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
      {
      rb.AddForce(gravity * lowJumpMultiplier, ForceMode.Acceleration);
      }
      else
      {
      rb.AddForce(gravity, ForceMode.Acceleration);
      }
      }
      }

  • @gabrielgp0811
    @gabrielgp0811 4 роки тому +2

    Scripts of video below
    using UnityEngine;
    using System.Collections;
    public class Jump : MonoBehaviour
    {
    [Range(1, 10)]
    public float jumpVelocity;
    Rigidbody2D rb;
    bool jumpRequest;
    void Awake()
    {
    rb = GetComponent();
    }
    void Update()
    {
    if (Input.GetButtonDown("Jump"))
    jumpRequest = true;
    }
    void FixedUpdate()
    {
    if (jumpRequest)
    {
    //rb.velocity += Vector2.up * jumpVelocity;
    rb.AddForce(Vector2.up * jumpVelocity, ForceMode2D.Impulse);
    jumpRequest = false;
    }
    }
    }
    using UnityEngine;
    using System.Collections;
    public class BetterJump : MonoBehaviour
    {
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    Rigidbody2D rb;
    void Awake()
    {
    rb = GetComponent();
    }
    void FixedUpdate()
    {
    if (rb.velocity.y < 0f)
    rb.gravityScale = fallMultiplier;
    else if (rb.velocity.y > 0f && !Input.GetButton("Jump"))
    rb.gravityScale = lowJumpMultiplier;
    else
    rb.gravityScale = 1f;
    }
    }

  • @JoshRhoton
    @JoshRhoton 4 роки тому

    Wow, 5 seconds before the first ad!?!? How stupid!!

  • @lewis571
    @lewis571 6 років тому +4

    First! Do I get a cookie? :)