C#
[C#][Unity] 점프 샘플 코드
teamnova
2022. 11. 5. 12:00
728x90
유니티에서 자주 쓰이는 점프 스크립트입니다.
여러 방식이 있겠으나, 가장 간단한 방법중 하나입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jump : MonoBehaviour
{
public float jumpHeight = 7f;
public bool isGrounded;
public float NumberJumps = 0f;
public float MaxJumps = 2;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (NumberJumps > MaxJumps - 1)
{
isGrounded = false;
}
if (isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpHeight);
NumberJumps += 1;
}
}
}
void OnCollisionEnter(Collision other)
{
isGrounded = true;
NumberJumps = 0;
}
void OnCollisionExit(Collision other)
{
}
}