Unity
점프 기능 구현
코딩 화이팅
2023. 10. 17. 13:16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using : import
public class BirdJump_1 : MonoBehaviour
//: MonoBehaviour: 라는 걸 상속 받는다.
{
Rigidbody2D rb;
public float jumpPower;
//public으로 선언하면 유니티에서도 값을 조절할 수 있음
//Start is called before the first frame update
void Start()
{
rb=GetComponent<Rigidbody2D>();
//Rigidbody2D라는 컴포넌트를 들고와서 rb에 담겠다.
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
//Input.GetMouseButtonDown : 마우스를 눌렀을 때
//Input.GetMouseButton(0) : 왼쪽 마우스를 눌렀을 때
{
rb.velocity = Vector2.up * jumpPower; // (0,1)
//velocity: 속도
//Vector2:2d
//Vector2.up : (0,1)
}
}
}
이렇게 작성하고 유니티로 돌아가 재생 버튼을 누르면 클릭을 할 때마다 새가 위로 올라간다.
여기의 Jump Power의 값을 수정하면 그 값에 맞게 점프력이 상승된다. 이것을 할 수 있는 이유는 위의 코드 안에 주석으로 설명을 달아놨다.