120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
public class PlayerMove : MonoBehaviour
|
|
{
|
|
public float maxSpeed;
|
|
public float jumpPower;
|
|
Rigidbody2D _rigidBody;
|
|
SpriteRenderer _spriteRenderer;
|
|
|
|
// TODO : 점프, 공격, 돌진, 슈퍼 대시
|
|
Animator _animator;
|
|
|
|
// 변수
|
|
[SerializeField]
|
|
public int hp = 5;
|
|
|
|
// 사용하는 컴포넌트들 초기화
|
|
void Awake()
|
|
{
|
|
_rigidBody = GetComponent<Rigidbody2D>();
|
|
_spriteRenderer = GetComponent<SpriteRenderer>();
|
|
_animator = GetComponent<Animator>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 점프
|
|
if (Input.GetButtonDown("Jump"))
|
|
{
|
|
_rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
|
|
}
|
|
|
|
// 키 입력 땔때 캐릭터 멈춤
|
|
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;
|
|
}
|
|
}
|
|
|
|
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.linearVelocityY < 0)
|
|
{
|
|
Debug.DrawRay(_rigidBody.position, Vector2.down, Color.red);
|
|
RaycastHit2D rayHit = Physics2D.Raycast(_rigidBody.position, Vector2.down, 0.1f);
|
|
if(rayHit.collider != null)
|
|
{
|
|
// // 바닥에 닿아있을 때 애니메이션 변경
|
|
if(rayHit.distance < 0.5f)
|
|
{
|
|
_animator.SetBool("isJumping", false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (collision.gameObject.tag == "Enemy")
|
|
{
|
|
// 적과 충돌했을 때
|
|
OnDameged(collision.transform.position);
|
|
Debug.Log("플레이어가 맞았습니다.");
|
|
}
|
|
}
|
|
|
|
void OnDameged(Vector2 targetPosition)
|
|
{
|
|
// 적과 충돌 후 무적 ( 11번 레이어 PlayerDamaged로 변경 )
|
|
// 물리 설정에서 PlayerDamaged 레이어와 Enemy 레이어가 충돌하지 않도록 설정함
|
|
gameObject.layer = 11;
|
|
|
|
// 플레이어 HP 감소
|
|
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); // 플레이어 반투명
|
|
|
|
}
|
|
} |