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); } } }
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.
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?
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.
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.
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.
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.
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?
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);
}
}
}
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.
This just saved my life. I could not figure out why my character sometimes did not jump. thank you so much!!!
It's *better* better jumping code
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?
You've messed up with Grammer
@@1e1001 *Grammar
@@aquaarmour4924 shite
Thank you very much for updating this!
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.
You can make it one.
This channel must be more popular!
Aren't you still checking input in the fixedUpdate though? Jump key is being checked
I also thought this, could someone explain please?
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.
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.
If gravity is only normal when the y speed is 0, it makes no difference to set it back?
How do you reset the gravity in 3d?
yeah that is my question :)
something like Physics.gravity = new Vector3(0,0,0)
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.
This saved my life thx.
This was really useful! Thank you! Are you going to do a video for moving vertically as well?
+André Odendaal I have a series on making a character controller as well that might meet your needs
Does it work with this better jump code?
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?
You fixed my issue, thanks a lot!
not caching the rigidbody?
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.
how to add double jump on that code?
Great Video man.
Hope I can someday reach the same audience as you.
It is not useful ... it is necessary to detect the "Ground" before jumping.
Please can you make it also for 3D
haha, this is how 4 lines become a big fat chunk of code
Allright.. thanks for the Tutorial. But its just working with 2d!! There is no gravityScale for 3D rigidbody. Should be more complex
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);
}
}
}
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;
}
}
Wow, 5 seconds before the first ad!?!? How stupid!!
First! Do I get a cookie? :)
:cookie: