본문 바로가기
> Unity/챌린지

김장 게임을 만들어보기 (7) - 플레이어 점프 2

by 메론소다 2025. 1. 22.
728x90
반응형
플레이어 물리법칙 적용

플레이어 이름 변경

왼쪽 패널에 녹색 박스 생긴거 확인.


플레이어가 점프하게 만들기
- 스크립트

행동 스크립트를 생성해보기.

Player 라고 이름 지정

위와 같이 작성.

연결해주기.

 


점프 계속 가능 오류 고치기

&& 사용

따라 적기

플랫폼과 부딪힌 것.

using UnityEngine;

public class Player : MonoBehaviour
{
    [Header("Settings")]
    public float JumpForce;

    [Header("References")]
    public Rigidbody2D PlayerRigidBody;

    private bool isGrounded = true;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
            PlayerRigidBody.AddForceY(JumpForce, ForceMode2D.Impulse);
            isGrounded = false;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.name == "Platform") {
            isGrounded = true;
        }
    }
}

728x90
반응형