Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I make a RigidBody Jump Faster?

Discussion in 'Scripting' started by cheapcpps, Dec 13, 2020.

  1. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    Hello. I am making a game with a simple character controller with jumping, however, the jump currently takes 1.240 seconds to finish, when I'd like it to be faster, so, how may I add the entire process of the jump faster? By that, I mean, making adding the force faster, and falling faster with the RigidBody.

    Here's my jump code (Don't criticize it please lol, just focus on the adding force part)
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.Space)) {
    4.             if (Time.time > CooldownTime) {
    5.                 Debug.Log("jump");
    6.                 PlayerRB.AddForce(Vector3.up * jumpForce * 157);
    7.                 anim.Play("Spinspon");
    8.                 CooldownTime = Time.time + 1.1f;
    9.             }
    10.         }
    11.     }
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    To increase the speed of the initial jump start you can use ForceMode.

    https://docs.unity3d.com/ScriptReference/ForceMode.html

    For returning, you can use a constant downward force that’s applied if the player is jumping. Using a bool to determine if he’s jumped. That way the applied downward force isn’t constantly being applied when it isn’t needed.
     
  3. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    Try using AddImpulse or using a wayyyyy larger number for the force. You can also modify the universal gravity to impact the fall rate, potentially wiggling with their mass properties as well.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    First of all, you want to use the correct ForceMode for jumps. Jumping, similar to explosions, can be expressed as an impulse, ie applying all the force at the same moment. The default ForceMode expects the force to be applied over multiple frames, like with gravity. So use ForceMode.Impulse for jumping.
    Using a Rigidbody means you want things to be handled using physics. However, in games we oftentimes do not want realism. To have more control over the jump, you can split it up into two halfs, rising and falling, or in other terms velocity.y > 0 and velocity.y <= 0. This allows you to add different multipliers and in general gives you more control over the jump. Oftentimes in popular games (especially 2D), the character falls a bit faster than it rises. This video may be helpful in that regard:

    The guy in the video also does not use AddForce at all and instead applies the velocity changes himself, which is also viable. For even more control over what your character does, you may want to get away from using physics and instead handle more of the movement manually (which, to be fair, is already what is being done in the video for the most part).
     
    JJunior, tree_arb and SparrowGS like this.
  5. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    Sorry, the ForceMode Documentation was confusing me, I saw AddForce(m_NewForce, ForceMode.Impulse);
    But how does that help me get the exact speed I want it to be, and for using a constant downward force, that's way more complicated than anything I've really done, so how do I do that
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    To get the right speed, just tweak the jump force value, which I assume is in the Inspector. You shouldn't need to multiply by two scale factors, just one (unless you want other objects to use one of the factors as well). For falling, simplest is to change overall gravity in the Project Settings---Physics window. If this creates issues globally, you can also experiment with linear drag of other rigidbodies, which will cause them to fall slower with higher values.
     
  7. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I wouldn’t recommend changing the overall factor in the project settings. If you use a bool to determine if the player is grounded (either by ray cast or collider detection) you can leave default settings and use a custom “gravity effect” with code. An example Code would be something like

    Code (csharp):
    1.  
    2.  
    3. if(!grounded)
    4. {
    5.       transform.Translate(-Vector3.up * gravity);
    6. }
    7.  
    8.  
    that’s a very simplistic way of doing it. Though I always code my “physics” and it usually contains a bit more than above. The above should justify your needs. I do not use rigid body though. I believe with rigid body it is better to use RigidBody.MovePosition.
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    My understanding is that it's best to use AddForce, leaving the ForceMode to default (continuous), as it's being applied every frame. Should work fine.
     
  9. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I was referring to my example. Instead of “translate” use “MovePosition”.

    If you are not using physics the RigidBody.MovePosition is best. If you are using physics, then RigidBody.AddForce.

    ForceMode can be changed... it’s still using “addforce” but a different type of force. Resulting in different results on how the RigidBody reacts.
     
    Yoreki likes this.