Created
April 21, 2020 12:55
-
-
Save IamGroooooot/8f37404d9cbac09ac3c045f44f7001f8 to your computer and use it in GitHub Desktop.
Simple Jump - using position (Unity)
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
using UnityEngine; | |
public class CustomJump : MonoBehaviour | |
{ | |
public float jumpPower = 5f; // 점프 Force | |
private const float GRAVITY = -9.81f; // 중력 가속도 | |
private Vector3 vel; // 현재 속도 | |
private Vector3 acc; // 현재 가속도 | |
private void Start() | |
{ | |
vel = Vector3.zero; | |
} | |
void Update() | |
{ | |
// 중력 설정 | |
acc = new Vector3(0, GRAVITY, 0); | |
// 스페이스바 누르면 점프! | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
vel.y = jumpPower; | |
} | |
// 가속도 적용 | |
vel += acc * Time.deltaTime; | |
// 땅으로 떨어지거나 하늘로 날아가지 않습니다 | |
if(transform.position.y < -0.5f) | |
{ | |
if (vel.y < 0) vel = Vector3.zero; | |
} | |
else if(transform.position.y > 5) | |
{ | |
if (vel.y > 0) vel = Vector3.zero; | |
} | |
// 속도만큼 이동시킨다 | |
transform.Translate(vel * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment