Created
October 5, 2020 18:07
-
-
Save saturngamesss/af64dd77cf30935325a8e47df8e68204 to your computer and use it in GitHub Desktop.
Simple 2D swipe jump for Unity Engine
This file contains hidden or 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
//************** 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 SwipeJump : MonoBehaviour | |
{ | |
Vector2 startTouchPosition, endTouchPosition; | |
Rigidbody2D rb; | |
float jumpForce = 700f; | |
bool jumpAllowed = false; | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
void Update() | |
{ | |
SwipeCheck(); | |
} | |
void FixedUpdate() | |
{ | |
JumpIfAllowed(); | |
} | |
private void SwipeCheck() | |
{ | |
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) | |
startTouchPosition = Input.GetTouch(0).position; | |
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) | |
{ | |
endTouchPosition = Input.GetTouch(0).position; | |
if (endTouchPosition.y > startTouchPosition.y && rb.velocity.y == 0) | |
jumpAllowed = true; | |
} | |
} | |
void JumpIfAllowed() | |
{ | |
if (jumpAllowed) | |
{ | |
rb.AddForce(Vector2.up * jumpForce); | |
jumpAllowed = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment