using System; using UnityEngine; public class IdleState : IState { public void Enter() { } public void Update() { } public void Exit() { } public IState CheckTransition() { // 우선 순위 // Jump를 우선 // Move는 Jump보다 낮은 우선 throw new NotImplementedException(); } } 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 jumpPower; Rigidbody2D _rigidBody; SpriteRenderer _spriteRenderer; // TODO : 점프, 공격, 돌진, 슈퍼 대시 Animator _animator; // 변수 [SerializeField] public int hp = 5; // 사용하는 컴포넌트들 초기화 void Awake() { _rigidBody = GetComponent(); _spriteRenderer = GetComponent(); _animator = GetComponent(); } private void Update() { // 점프 ( 연속 점프 방지 ) // TODO : C# 최적화 방법 중 1가지 > GC.Alloc 최소화 // string >> class변수 (힙 메모리 저장), 가비지 컬렉터에 의해 제거됨 // struct >> 스택 메모리로 사용하지 않으면 바로 제거됨 if (Input.GetButtonDown("Jump") && _animator.GetBool(IsJumping) == false) { _rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse); _animator.SetBool(IsJumping, true); } // 키 입력 땔때 캐릭터 멈춤 if (Input.GetButtonUp("Horizontal")) { _rigidBody.linearVelocity = new Vector2(_rigidBody.linearVelocity.normalized.x * 0.0000001f, _rigidBody.linearVelocity.y); } // 캐릭터(Sprite)이동 방향 바라보도록 스프라이트 플립 if (Input.GetButton("Horizontal")) { _spriteRenderer.flipX = Input.GetAxisRaw("Horizontal") >= 1; } // 애니메이션 설정 if (MathF.Abs(_rigidBody.linearVelocity.x) < 0.5) { _animator.SetBool(IsMoveing, false); } else { _animator.SetBool(IsMoveing, true); } } void FixedUpdate() { // 캐릭터 움직임 컨트롤 float h = Input.GetAxisRaw("Horizontal"); _rigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse); // 유니티6 부터 Velocity에서 LinearVelocity로 변경 if (_rigidBody.linearVelocityX > maxSpeed) // 오른쪽 최대 속도 { _rigidBody.linearVelocity = new Vector2(maxSpeed, _rigidBody.linearVelocity.y); } else if (_rigidBody.linearVelocityX < maxSpeed * (-1)) { _rigidBody.linearVelocity = new Vector2(maxSpeed * (-1), _rigidBody.linearVelocity.y); } // 플랫폼을 밟고 있을 때 if (_rigidBody.linearVelocity.y < 0) { Debug.DrawRay(_rigidBody.position, Vector3.down, Color.green); RaycastHit2D rayHit = Physics2D.Raycast(_rigidBody.position, Vector3.down, 1, LayerMask.GetMask("Platform")); if(rayHit.collider != null) { // // 바닥에 닿아있을 때 애니메이션 변경 if(rayHit.distance < 0.7f) { _animator.SetBool(IsJumping, false); } } } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy")) { // 적과 충돌했을 때 OnDameged(collision.transform.position); Debug.Log("플레이어가 맞았습니다."); } } void OnDameged(Vector2 targetPosition) { // 적과 충돌 후 무적 ( 11번 레이어 PlayerDamaged로 변경 ) // 물리 설정에서 PlayerDamaged 레이어와 Enemy 레이어가 충돌하지 않도록 설정함 gameObject.layer = 11; // 플레이어 HP 감소 hp--; Debug.Log(hp); // 무적 표시 _spriteRenderer.color = new Color(1,1,1,0.5f); // 플레이어 반투명 // 적과의 충돌(피격 시) 뒤로 밀려남 int direction = transform.position.x - targetPosition.x > 0 ? 1 : -1; // 적과의 상대 위치에 따라 방향 결정 if (direction == -1) { _rigidBody.AddForce(Vector2.left * 5, ForceMode2D.Impulse); } else { _rigidBody.AddForce(Vector2.right * 5, ForceMode2D.Impulse); } Invoke("OffDamaeged", 1); } void OffDamaeged() { gameObject.layer = 10; _spriteRenderer.color = new Color(1,1,1,1); // 플레이어 반투명 } }