코드 정리

This commit is contained in:
Mingu Kim
2025-07-22 03:01:57 +09:00
parent d910fd464c
commit 325f2d4785

View File

@@ -2,6 +2,9 @@ using System;
using UnityEngine; using UnityEngine;
public class PlayerMove : MonoBehaviour public class PlayerMove : MonoBehaviour
{ {
private static readonly int IsJumping = Animator.StringToHash("isJumping");
private static readonly int IsMoveing = Animator.StringToHash("isMoveing");
public float maxSpeed; public float maxSpeed;
public float jumpPower; public float jumpPower;
Rigidbody2D _rigidBody; Rigidbody2D _rigidBody;
@@ -25,10 +28,14 @@ public class PlayerMove : MonoBehaviour
private void Update() private void Update()
{ {
// 점프 ( 연속 점프 방지 ) // 점프 ( 연속 점프 방지 )
if (Input.GetButtonDown("Jump") && _animator.GetBool("isJumping") == false) // TODO : C# 최적화 방법 중 1가지 > GC.Alloc 최소화
// string >> class변수 (힙 메모리 저장), 가비지 컬렉터에 의해 제거됨
// struct >> 스택 메모리로 사용하지 않으면 바로 제거됨
if (Input.GetButtonDown("Jump") && _animator.GetBool(IsJumping) == false)
{ {
_rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse); _rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
_animator.SetBool("isJumping", true); _animator.SetBool(IsJumping, true);
} }
// 키 입력 땔때 캐릭터 멈춤 // 키 입력 땔때 캐릭터 멈춤
@@ -46,11 +53,11 @@ public class PlayerMove : MonoBehaviour
// 애니메이션 설정 // 애니메이션 설정
if (MathF.Abs(_rigidBody.linearVelocity.x) < 0.5) if (MathF.Abs(_rigidBody.linearVelocity.x) < 0.5)
{ {
_animator.SetBool("isMoveing", false); _animator.SetBool(IsMoveing, false);
} }
else else
{ {
_animator.SetBool("isMoveing", true); _animator.SetBool(IsMoveing, true);
} }
} }
@@ -82,7 +89,7 @@ public class PlayerMove : MonoBehaviour
// // 바닥에 닿아있을 때 애니메이션 변경 // // 바닥에 닿아있을 때 애니메이션 변경
if(rayHit.distance < 0.7f) if(rayHit.distance < 0.7f)
{ {
_animator.SetBool("isJumping", false); _animator.SetBool(IsJumping, false);
} }
} }
} }
@@ -90,7 +97,7 @@ public class PlayerMove : MonoBehaviour
void OnCollisionEnter2D(Collision2D collision) void OnCollisionEnter2D(Collision2D collision)
{ {
if (collision.gameObject.tag == "Enemy") if (collision.gameObject.CompareTag("Enemy"))
{ {
// 적과 충돌했을 때 // 적과 충돌했을 때
OnDameged(collision.transform.position); OnDameged(collision.transform.position);