Skip to content

Instantly share code, notes, and snippets.

@alexson
Created January 13, 2022 09:44
Show Gist options
  • Save alexson/44ec17dfdda50e1874784ee697673047 to your computer and use it in GitHub Desktop.
Save alexson/44ec17dfdda50e1874784ee697673047 to your computer and use it in GitHub Desktop.
public class BombBehaviour : MonoBehaviour
{
[Header("State")]
public bool exploded;
[Header("Object")]
public GameObject bombRoot;
[Header("Explosion")]
public GameObject explosionEffect;
public float explodeTime = 1;
private float countingDownNow;
private float countDownTotal;
// Start is called before the first frame update
void Start()
{
countDownTotal = explodeTime;
//Facing Camera
transform.LookAt(Camera.main.transform);
transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y + Random.Range(-20, 20), 0);
}
// Update is called once per frame
void FixedUpdate()
{
Counting();
}
public void Explode()
{
if (!exploded)
{
exploded = true;
//Debug.Log("Explode");
//Spawn effect
GameObject exEff = Instantiate(explosionEffect, transform.position, transform.rotation);
exEff.transform.position = bombRoot.transform.position;
exEff.transform.localScale = transform.localScale;
exEff.SetActive(true);
//Destroy this object
Destroy(gameObject);
}
}
public void Counting()
{
if (countingDownNow < countDownTotal)
countingDownNow += Time.deltaTime;
else
Explode();//count reached
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment