Files
Project-AI-Chronicle/unity-client/Assets/Scripts/PlayerMove.cs
T

108 lines
2.7 KiB
C#

using UnityEngine;
public struct Inputs
{
public Vector2 PlayerMove;
}
public class InputContoller
{
public static void UpdateInput(ref Inputs input)
{
input.PlayerMove.x = Input.GetAxisRaw("Horizontal");
input.PlayerMove.y = Input.GetAxisRaw("Vertical");
}
}
public class PlayerMove : MonoBehaviour
{
// 컴포넌트 프로퍼티
public Rigidbody2D rb { get; private set; }
// [수정] 동일한 오브젝트에 있는 SPUM_Prefabs를 가져옵니다.
private SPUM_Prefabs spumPrefab;
private float maxSpeed;
private Inputs _inputs;
public Inputs Inputse => _inputs;
// 이동 관련 변수
[SerializeField] private float speed = 5f;
[SerializeField] public int hp = 5;
private bool isFacingRight = false;
private int currentAnimationState = -1;
private Animator animator;
void Awake()
{
// 1. Rigidbody2D 할당
rb = GetComponent<Rigidbody2D>();
// 2. [수정] 같은 오브젝트에 붙어있으므로 GetComponent로 정확히 가져옵니다.
spumPrefab = GetComponentInParent<SPUM_Prefabs>();
// spumPrefab = GetComponent<SPUM_Prefabs>();
if (spumPrefab == null)
{
Debug.LogError($"{gameObject.name} 오브젝트에 SPUM_Prefabs 컴포넌트가 없습니다! 인스펙터를 확인하세요.");
}
// 캐릭터 방향 기본 오른쪽 설정
Vector3 initialScale = transform.localScale;
initialScale.x = Mathf.Abs(initialScale.x);
transform.localScale = initialScale;
animator = GetComponentInChildren<Animator>();
}
private void Update()
{
InputContoller.UpdateInput(ref _inputs);
// 좌우 방향 전환 (Flip)
if (_inputs.PlayerMove.x < 0f && isFacingRight)
{
Flip();
}
else if (_inputs.PlayerMove.x > 0f && !isFacingRight)
{
Flip();
}
// 애니메이션 업데이트 호출
if (_inputs.PlayerMove.x != 0f || _inputs.PlayerMove.y != 0f)
{
animator.SetBool("1_Move", true);
}
else
{
animator.SetBool("1_Move", false);
}
if (Input.GetKeyDown(KeyCode.Z))
{
animator.SetTrigger("2_Attack");
}
}
void FixedUpdate()
{
Move();
}
private void Move()
{
if (rb == null) return;
rb.linearVelocity = new Vector2(_inputs.PlayerMove.x * speed, rb.linearVelocity.y);
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 scale = transform.localScale;
scale.x *= -1f;
transform.localScale = scale;
}
}