public class PlayerMovment : MonoBehaviour
{
// ตัวแปรที่เพิ่มเข้ามา
public int playerHelath = 100;
public float shootDelayTime = 0.5f;
public UIController uiController;
public int currentBullet;
public int bulletPerMag = 15;
private float shootCountTime = 0.0f;
private bool isReadyToShoot;
void Start()
{
rb = GetComponent<Rigidbody>();
currentBullet = bulletPerMag;
}
void Update()
{
shootCountTime -= Time.deltaTime;
isReadyToShoot = shootCountTime <= 0f;
if (CrossPlatformInputManager.GetButton("Fire") && isReadyToShoot)
{
if (currentBullet <= 0)
{
// reload
return;
}
GameObject obj = ObjectPooling.current.GetPooledObject();
if (obj == null)
{
return;
}
obj.transform.position = bulletPosition.position;
obj.transform.rotation = bulletPosition.rotation;
obj.SetActive(true);
shootCountTime = shootDelayTime;
--currentBullet;
}
// Update UI
uiController.SetPlayerHealth(playerHelath);
uiController.SetBullet(currentBullet, bulletPerMag);
}
void FixedUpdate()
{
moveDirection = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), 0 , CrossPlatformInputManager.GetAxis("Vertical"));
Move();
Turn();
}
void Move()
{
// Code เดิม
}
void Turn()
{
// Code เดิม
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIController : MonoBehaviour
{
public Text playerHealthText;
public Text bulletText;
public void SetPlayerHealth(int health)
{
playerHealthText.text = health.ToString();
}
public void SetBullet(int currentBullet, int bulletPerMag)
{
bulletText.text = currentBullet + "/" + bulletPerMag;
}
}