Files
WildRoot/Assets/Script/PlayerController.cs
T

91 lines
2.3 KiB
C#
Raw Normal View History

2026-04-14 17:06:58 +09:00
using UnityEngine;
using System.Collections;
2026-04-14 17:06:58 +09:00
public class PlayerController : MonoBehaviour
{
[Header("Movement")]
2026-04-14 17:06:58 +09:00
public float speed = 8f;
private Rigidbody playerRigidbody;
public VirtualJoystick joystick;
2026-04-14 17:06:58 +09:00
[Header("Shield System")]
public int shieldCount = 3;
private bool isShieldActive = false;
public GameObject shieldVisual;
public ShieldUIHandler shieldUI; // UI 전용 스크립트 참조
void Start()
2026-04-14 17:06:58 +09:00
{
playerRigidbody = GetComponent<Rigidbody>();
// 초기 UI 상태 반영
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
2026-04-14 17:06:58 +09:00
}
void Update()
2026-04-14 17:06:58 +09:00
{
HandleMovement();
// PC 입력 처리 (모바일 아닐 때만)
if (!GameSettings.IsMobile && Input.GetKeyDown(KeyCode.Alpha1))
{
ActivateShieldLogic();
}
2026-04-14 17:06:58 +09:00
}
void HandleMovement()
2026-04-14 17:06:58 +09:00
{
float x = 0, z = 0;
if (GameSettings.IsMobile && joystick != null)
{
Vector2 input = joystick.GetInput();
x = input.x; z = input.y;
}
else
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
}
2026-04-14 17:06:58 +09:00
playerRigidbody.linearVelocity = new Vector3(x * speed, 0f, z * speed);
2026-04-14 17:06:58 +09:00
if (x != 0 || z != 0)
2026-04-15 12:59:37 +09:00
{
transform.rotation = Quaternion.LookRotation(new Vector3(x, 0f, z));
2026-04-14 17:06:58 +09:00
}
2026-04-15 12:59:37 +09:00
// 애니메이션
2026-04-15 12:59:37 +09:00
Animator anim = GetComponentInChildren<Animator>();
if (anim != null) anim.SetBool("isMoving", (x != 0 || z != 0));
2026-04-14 17:06:58 +09:00
}
public void ActivateShieldLogic()
2026-04-14 17:06:58 +09:00
{
if (shieldCount > 0 && !isShieldActive)
2026-04-14 17:06:58 +09:00
{
StartCoroutine(ActivateShieldRoutine());
}
}
2026-04-14 17:06:58 +09:00
private IEnumerator ActivateShieldRoutine()
2026-04-14 17:06:58 +09:00
{
isShieldActive = true;
shieldCount--;
// UI 업데이트 위임
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
if (shieldVisual != null) shieldVisual.SetActive(true);
yield return new WaitForSeconds(2f);
if (shieldVisual != null) shieldVisual.SetActive(false);
isShieldActive = false;
2026-04-14 17:06:58 +09:00
}
public void Die()
{
gameObject.SetActive(false);
FindFirstObjectByType<GameManager>()?.EndGame();
}
}