บทความนี้จะเกี่ยวกับการ Reload กระสุน และ สร้างปุ่ม Reload กระสุนนะครับ
ซึ่งจะมีการใช้งาน Coroutine เพื่อช่วยในการ Delay โหลดกระสุนนะครับ
สามารถดู Coroutine เพิ่มเติมได้ที่
http://docs.unity3d.com/Manual/Coroutines.html
http://docs.unity3d.com/ScriptReference/Coroutine.html
// ตัวแปรที่เพิ่มเข้ามา
public float delay = 1.0f;
private bool isReload = false;
// function ที่เพิ่ม และ แก้ไข
void Update()
{
shootCountTime -= Time.deltaTime;
isReadyToShoot = shootCountTime <= 0f;
if (currentBullet <= 0)
{
// reload
StartCoroutine(Reload());
return;
}
if (CrossPlatformInputManager.GetButton("Fire") && isReadyToShoot && !isReload)
{
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;
}
if (CrossPlatformInputManager.GetButtonDown("Reload") && !isReload)
{
// reload
StartCoroutine(Reload());
}
uiController.SetPlayerHealth(playerHelath);
uiController.SetBullet(currentBullet, bulletPerMag);
}
private IEnumerator Reload()
{
if (!isReload)
{
isReload = true;
yield return new WaitForSeconds(delay);
currentBullet = bulletPerMag;
isReload = false;
}
}
No Game No Dream !!!!