Files
WildRoot/Assets/Script/PlayerController.cs
T

124 lines
3.1 KiB
C#
Raw Normal View History

2026-04-14 17:06:58 +09:00
using TMPro;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2026-04-14 17:06:58 +09:00
public class PlayerController : MonoBehaviour
{
private Rigidbody playerRigidbody;
public float speed = 8f;
public int shieldCount = 3;
private bool isShieldActive = false;
public GameObject shieldVisual;
public Image shieldIcon;
public TextMeshProUGUI countText;
// [추가] 조이스틱 연결용 변수
public VirtualJoystick joystick;
2026-04-14 17:06:58 +09:00
public void Die()
{
gameObject.SetActive(false);
GameManager gm = FindFirstObjectByType<GameManager>();
gm.EndGame();
}
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
if (GameSettings.IsMobile)
{
if (shieldIcon != null) shieldIcon.gameObject.SetActive(false);
if (countText != null) countText.gameObject.SetActive(false);
}
else
{
UpdateShieldUI();
}
2026-04-14 17:06:58 +09:00
}
void Update()
{
float x = 0;
float 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
float xSpeed = x * speed;
float zSpeed = z * speed;
2026-04-15 12:59:37 +09:00
bool moveInput = (x != 0 || z != 0);
2026-04-14 17:06:58 +09:00
Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed);
playerRigidbody.linearVelocity = newVelocity;
if (moveInput)
2026-04-15 12:59:37 +09:00
{
Vector3 moveDirection = new Vector3(x, 0f, z);
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
2026-04-16 15:04:32 +09:00
transform.rotation = newRotation;
2026-04-15 12:59:37 +09:00
}
// PC일 때만 키보드 실드 입력 허용
if (!GameSettings.IsMobile && Input.GetKeyDown(KeyCode.Alpha1))
2026-04-14 17:06:58 +09:00
{
ActivateShieldLogic();
2026-04-14 17:06:58 +09:00
}
2026-04-15 12:59:37 +09:00
Animator anim = GetComponentInChildren<Animator>();
if (anim != null)
{
anim.SetBool("isMoving", moveInput);
}
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(ActivateShield());
}
}
2026-04-14 17:06:58 +09:00
void UpdateShieldUI()
{
if (countText != null)
{
countText.text = shieldCount.ToString();
2026-04-14 17:06:58 +09:00
if (shieldCount <= 0)
{
countText.gameObject.SetActive(false);
2026-04-14 17:06:58 +09:00
if (shieldIcon != null)
{
shieldIcon.color = new Color(0.2f, 0.2f, 0.2f, 0.5f);
}
}
}
}
IEnumerator ActivateShield()
2026-04-14 17:06:58 +09:00
{
isShieldActive = true;
shieldCount--;
UpdateShieldUI();
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
}
}