ok that worked when I use box collider but only when I go straight threw it and not smash through the object
I have a script on the object that allow me to smash threw it
cause im using this tutorial
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destructible : MonoBehaviour {
public GameObject destroyedVersion;
void OnCollisionEnter()
{
Instantiate (destroyedVersion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
It would be better to just spawn in an entire new cube, so we dont have to worry about ‘broken’ objects.
Create an empty GameObject and add the BoxSpawner onto it.
Everytime a box despawns, it’ll invoke the spawner and generate a new box.
using System;
using System.Collections;
using UnityEngine;
public class Box : MonoBehaviour {
void OnTriggerEnter(Collider col) {
StartCoroutine(Respawn(6f, 10f));
}
IEnumerator Respawn(float timeToDespawn, float timeToRespawn) {
yield return new WaitForSeconds(timeToDespawn);
BoxSpawner.Respawn(timeToRespawn);
Destroy(gameObject);
}
private void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
StartCoroutine(Respawn(1f, 2f));
}
}
}
using System;
using System.Collections;
using UnityEngine;
public class BoxSpawner : MonoBehaviour {
// Assign box prefab.
[SerializeField]
private GameObject box;
// Access-point for Box script.
private static BoxSpawner instance;
// Spawn the box when the game Starts.
void Start() {
instance = this;
StartCoroutine(SpawnBox(0f));
}
IEnumerator SpawnBox(float respawnTime) {
yield return new WaitForSeconds(respawnTime);
Instantiate(box, transform.position, transform.rotation);
}
public static void Respawn(float respawnTime) {
instance.StartCoroutine(instance.SpawnBox(respawnTime));
}
}
Im confused when you say add BoxSpawner im assuming you mean the script
also when I add the script what wall do u want me to add to Box the non cracked or the cracked one.
Im still a little lost on the box spawner script do you want me to add wall no cracked or wall cracked to Box. cause I both but they just spawner another object straight away
Okay, so you need to delete the box, just keep the spawner with the box prefab that isnt cracked.
When you start the game, it will spawn the box. Once in gets destroyed it’ll respawn.