В игре steamer unity есть граната, к которой прикреплен сценарий гранаты. Взаимодействующий компонент событий наведения также прикреплен к grenade, который вызывает функцию Explode() из файла Grenadde. Взрыв происходит сразу после освобождения предмета из рук. Необходимо реализовать взрыв гранаты, например, за 5 секунд`
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
using Valve.VR.InteractionSystem;
public class Grenadde : MonoBehaviour
{
InteractableHoverEvents interactableHoverEvents;
public float delay = 3f;
public float radius = 50f;
public float force = 700f;
public GameObject explosionEffect;
float countdown;
bool hasExploded = false;
private Interactable interactable;
public bool isGrabbed=false;
// Start is called before the first frame update
void Start()
{
countdown = delay;
}
// Update is called once per frame
public void Update()
{
if(interactable = null)
{
countdown -= Time.deltaTime;
if (countdown <= 0f && !hasExploded )
{
Explode();
hasExploded = true;
Debug.Log("Lolo");
} }
}
public void Explode()
{
/*countdown = delay;
countdown -= Time.deltaTime;
if (countdown <= 0f && !hasExploded)
{*/
Destroy(gameObject);
Instantiate(explosionEffect, transform.position, transform.rotation);
Collider[] overlappedColliders = Physics.OverlapSphere(transform.position, radius);
for (int i=0; i < overlappedColliders.Length; i++) {
Rigidbody rigidbody = overlappedColliders[i].attachedRigidbody;
if (rigidbody) {
rigidbody.AddExplosionForce(force, transform.position, radius);
Grenadded grenadded = rigidbody.GetComponent<Grenadded>();
if(grenadded){
if(Vector3.Distance(transform.position, rigidbody.position) < radius);
grenadded.Hp -= 100;
grenadded.Explode();
}
}
}
/*Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject in colliders)
{
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
}
}*/
}
//}
}