Last active
January 31, 2020 17:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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