BreakTheBox Updated box
This commit is contained in:
parent
da624c8c85
commit
8811b6a6fc
@ -4,57 +4,71 @@
|
||||
|
||||
public class CrashCrate : MonoBehaviour
|
||||
{
|
||||
[Header("Whole Crate")]
|
||||
public MeshRenderer wholeCrate;
|
||||
public BoxCollider boxCollider;
|
||||
|
||||
[Header("Fractured Crate")]
|
||||
public GameObject fracturedCrate; // parent object with all fractured pieces (initially disabled)
|
||||
public GameObject[] crackPiecesPerTap; // individual pieces to show before full break
|
||||
public GameObject[] crackPiecesPerTap;
|
||||
|
||||
[Header("Audio")]
|
||||
public AudioSource audioSource;
|
||||
public AudioClip crackSound;
|
||||
public AudioClip crashSound;
|
||||
|
||||
public Animator dogAnimator;
|
||||
|
||||
private int hitCount = 0;
|
||||
private const int maxHits = 5;
|
||||
public Animator dogAnimator;
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Make sure fractured crate and crack pieces are hidden at start
|
||||
fracturedCrate.SetActive(false);
|
||||
foreach (var piece in crackPiecesPerTap)
|
||||
{
|
||||
piece.SetActive(false);
|
||||
if (piece != null)
|
||||
{
|
||||
piece.SetActive(true); // ✅ keep visible
|
||||
var rb = piece.GetComponent<Rigidbody>();
|
||||
if (rb) rb.isKinematic = true; // ✅ freeze physics
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0)) // Tap or click
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
if (hit.collider.gameObject == gameObject)
|
||||
GameObject touchedObject = hit.collider.gameObject;
|
||||
|
||||
// Check if the tapped object is in our list
|
||||
for (int i = 0; i < crackPiecesPerTap.Length; i++)
|
||||
{
|
||||
RegisterHit();
|
||||
if (crackPiecesPerTap[i] == touchedObject)
|
||||
{
|
||||
RegisterHit(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterHit()
|
||||
private void RegisterHit(int index)
|
||||
{
|
||||
hitCount++;
|
||||
|
||||
if (hitCount < maxHits)
|
||||
{
|
||||
// Enable crack pieces one-by-one
|
||||
if (hitCount - 1 < crackPiecesPerTap.Length)
|
||||
var piece = crackPiecesPerTap[index];
|
||||
|
||||
var rb = piece.GetComponent<Rigidbody>();
|
||||
if (rb && rb.isKinematic)
|
||||
{
|
||||
crackPiecesPerTap[hitCount - 1].SetActive(true);
|
||||
rb.isKinematic = false;
|
||||
Vector3 explosionOrigin = transform.position + Random.insideUnitSphere * 0.5f;
|
||||
explosionOrigin.y = transform.position.y - 0.5f; // keep the force coming from slightly below
|
||||
|
||||
rb.AddExplosionForce(10f, explosionOrigin, 2f, 0.5f, ForceMode.Impulse);
|
||||
|
||||
}
|
||||
|
||||
if (crackSound && audioSource)
|
||||
@ -68,23 +82,37 @@
|
||||
|
||||
private void BreakCrate()
|
||||
{
|
||||
wholeCrate.enabled = false;
|
||||
boxCollider.enabled = false;
|
||||
fracturedCrate.SetActive(true);
|
||||
foreach (var piece in crackPiecesPerTap)
|
||||
{
|
||||
if (piece != null)
|
||||
{
|
||||
var rb = piece.GetComponent<Rigidbody>();
|
||||
if (rb && rb.isKinematic)
|
||||
{
|
||||
rb.isKinematic = false;
|
||||
rb.AddExplosionForce(300f, transform.position, 2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (crashSound && audioSource)
|
||||
audioSource.PlayOneShot(crashSound);
|
||||
dogAnimator.SetInteger("state", 1);
|
||||
Invoke(nameof(Destroyer),1);
|
||||
|
||||
if (dogAnimator)
|
||||
dogAnimator.SetInteger("state", 1);
|
||||
|
||||
Invoke(nameof(Destroyer), 1f);
|
||||
}
|
||||
void Destroyer()
|
||||
|
||||
private void Destroyer()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
[ContextMenu("Test")]
|
||||
|
||||
[ContextMenu("Test Hit Index 0")]
|
||||
public void Test()
|
||||
{
|
||||
RegisterHit();
|
||||
RegisterHit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user