Skip to content

Instantly share code, notes, and snippets.

@Marsgames
Last active January 31, 2020 17:17
Show Gist options
  • Save Marsgames/35377e9f35d923df38d48866dca165d3 to your computer and use it in GitHub Desktop.
Save Marsgames/35377e9f35d923df38d48866dca165d3 to your computer and use it in GitHub Desktop.
Unity version of Mario jumping physics. If you get jump key down you jump higher than just a press
// Source : https://www.youtube.com/watch?v=7KiK0Aqtmzc
#region Variables
[SerializeField] private float jumpVelocity = 5;
[SerializeField] private float fallOffMultiplier = 2.5f;
[SerializeField] private float lowJumpMultiplier = 2;
private Rigidbody2D rigidbody;
#endregion Variables
private void Jump (KeyCode jumpButton)
{
if (Input.GetKeyDown (jumpButton))
{
rigidbody.velocity = Vector2.up * jumpVelocity;
}
if (rigidbody.velocity.y < 0)
{
rigidbody.velocity += Vector2.up * (Physics2D.gravity.y * rigidbody.gravityScale) * (fallOffMultiplier - 1) * Time.deltaTime;
}
else if (rigidbody.velocity.y > 0 && !Input.GetKey (jumpButton))
{
rigidbody.velocity += Vector2.up * (Physics2D.gravity.y * rigidbody.gravityScale) * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment