Unity에서 자체 중력을 사용하지 않고 점프하는 방법
5515 단어 unity
//
if (Input.GetButtonDown("Jump") &&nextJump) //
if (currentBaseState.fullPathHash == walkingState||
currentBaseState.fullPathHash == runningState||
currentBaseState.fullPathHash == standingState)//
{
nextJump = false;//
GameManager.isJumping = true;//
if (GameManager.isStanding)
{
jumpV_x = 0;// 0
GameManager.isStanding = false;// ,
}
if (GameManager.isWalking)
{
jumpV_x = Haxis * moveSpeed;
GameManager.isWalking = false;
}
if (GameManager.isRunning) //
{
jumpV_x = Haxis * moveSpeed;
jumpVelocity = GameManager.jumpVelocity * GameManager.jumpMultiple;//
GameManager.isRunning = false;
}
}
점프 상태 ==true 시 프레임마다 상응하는 세로, 수평 거리 이동
private void LateUpdate()
{
transform.Translate(Vector3.right * Time.deltaTime * moveSpeed * Haxis); //
if (GameManager.isJumping) //
{
jumpHight += jumpVelocity * Time.deltaTime * jumpSpeed;
jumpVelocity = jumpVelocity - 9.8f * Time.deltaTime * jumpSpeed;
currentPosition.y = jumpHight;
currentPosition.x = privousPosition.x + Time.deltaTime * jumpV_x; //
transform.position = currentPosition;
}
착지 후 점프 상태에서 탈퇴, 다음 점프를 허용, 점프 속도의 전역 변수를 다음 점프를 위해 초기값으로 회귀
void OnCollisionEnter(Collision collider)
{
//
if (collider.gameObject.tag == "Ground")
{
nextJump = true;
GameManager.isGround = true;
GameManager.isJumping = false;
//
moveSpeed = GameManager.moveSpeed;
jumpVelocity = GameManager.jumpVelocity;
jumpHight = 0;
jumpV_x = 0;
Debug.Log("ground!");
}
}
void OnCollisionExit(Collision collider)
{
//
if (collider.gameObject.tag == "Ground")
GameManager.isGround = false;
Debug.Log("offground!");
}