Created
April 21, 2020 14:59
-
-
Save saturngamesss/49f5f27a1eb34d3da6f1ddd88e86434c to your computer and use it in GitHub Desktop.
Simple double jump code for Unity Engine
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
//FROM PROJECT ROROPE || REAL GAMES STUDIO | |
//************************************************ | |
//realgamesss.weebly.com | |
//gamejolt.com/@Real_Game | |
//realgamesss.newgrounds.com/ | |
//real-games.itch.io/ | |
//youtube.com/channel/UC_Adg-mo-IPg6uLacuQCZCQ | |
//************************************************ | |
using UnityEngine; | |
public class DoubleJump : MonoBehaviour | |
{ | |
Rigidbody2D rb; | |
public float speed = 50; | |
public float jumpForce = 20; | |
public bool isGrounded; | |
public bool isJumping; | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
void Update() | |
{ | |
if (Input.GetKey(KeyCode.D)) | |
{ | |
rb.velocity = new Vector2(speed, rb.velocity.y); | |
} | |
else if (Input.GetKey(KeyCode.A)) | |
{ | |
rb.velocity = new Vector2(-speed, rb.velocity.y); | |
} | |
else | |
{ | |
rb.velocity = new Vector2(0, rb.velocity.y); | |
} | |
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) | |
{ | |
rb.velocity = new Vector2(rb.velocity.x, jumpForce); | |
isJumping = true; | |
} | |
else if (Input.GetKeyDown(KeyCode.Space) && isJumping == true) | |
{ | |
rb.velocity = new Vector2(rb.velocity.x, jumpForce); | |
isJumping = false; | |
} | |
} | |
void OnCollisionEnter2D(Collision2D collision) | |
{ | |
if (collision.gameObject.CompareTag("Ground")) | |
{ | |
isGrounded = true; | |
} | |
} | |
void OnCollisionExit2D(Collision2D collision) | |
{ | |
isGrounded = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's worked, thanks!