Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
[RequireComponent(typeof(AudioSource))]
public class GMScript : MonoBehaviour
{
public GameObject bomb;
public GameObject explosionPrefab;
public int bombHealth = 10;
public TMP_Text bombHealthText;
public TMP_Text bombHealthNumber;
public float initialDelay = 10.0f;
public float repeatingRate = 0.5f;
public float allowNextKeyboardInput = 0.0f;
public AudioSource explosion;
// Start is called before the first frame update
void Start()
{
explosion = GetComponent();
}
// Update is called once per frame
void Update()
{
Vector3 pos = bomb.transform.position;
if(Input.GetKeyDown(KeyCode.Escape))
{
RemoveHealth();
allowNextKeyboardInput = Time.time + initialDelay;
}
else if(Time.time > allowNextKeyboardInput && Input.GetKey(KeyCode.Escape))
{
Debug.Log(Time.time.ToString());
Debug.Log(allowNextKeyboardInput.ToString());
RemoveHealth();
allowNextKeyboardInput += repeatingRate;
}
else if (Input.GetKey(KeyCode.Escape) == false)
{
allowNextKeyboardInput = 0f;
}
if(Input.GetKeyDown(KeyCode.Return))
{
AddHealth();
allowNextKeyboardInput = Time.time + initialDelay;
}
else if(Time.time > allowNextKeyboardInput && Input.GetKey(KeyCode.Return))
{
AddHealth();
allowNextKeyboardInput += repeatingRate;
}
else if (!Input.GetKey(KeyCode.Return))
{
allowNextKeyboardInput = 0f;
}
void RemoveHealth()
{
if (bombHealth != 0)
{
bombHealth -= 1;
{
UpdateText(bombHealth);
}
if (pos.x == 15)
{
Debug.Log("Bomb has been deleted already");
}
if (bombHealth == 0)
{
if (pos.x == 15)
{
Debug.Log("Bomb has been deleted already");
}
if (pos.x != 15)
{
explosion.Play(1);
DestroyObject(bomb);
}
}
}
}
void AddHealth()
{
bombHealth += 1;
UpdateText(bombHealth);
}
void UpdateText(int txt)
{
bombHealthNumber.text = txt.ToString();
}
void DestroyObject(GameObject obj)
{
Instantiate(explosionPrefab, pos, Quaternion.identity);
Vector3 bombNewPostion = new Vector3(15.0f ,0.0f ,0.0f);
bomb.transform.position = bombNewPostion;
Debug.Log(obj.name + "Has been deleted");
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -clickable
Мобильная версия