BreakTheBox Updated box

This commit is contained in:
Ali Sharoz 2025-07-15 14:22:42 +05:00
parent da624c8c85
commit 8811b6a6fc
2 changed files with 232 additions and 5937 deletions

View File

@ -4,57 +4,71 @@
public class CrashCrate : MonoBehaviour public class CrashCrate : MonoBehaviour
{ {
[Header("Whole Crate")]
public MeshRenderer wholeCrate;
public BoxCollider boxCollider;
[Header("Fractured Crate")] [Header("Fractured Crate")]
public GameObject fracturedCrate; // parent object with all fractured pieces (initially disabled) public GameObject[] crackPiecesPerTap;
public GameObject[] crackPiecesPerTap; // individual pieces to show before full break
[Header("Audio")] [Header("Audio")]
public AudioSource audioSource; public AudioSource audioSource;
public AudioClip crackSound; public AudioClip crackSound;
public AudioClip crashSound; public AudioClip crashSound;
public Animator dogAnimator;
private int hitCount = 0; private int hitCount = 0;
private const int maxHits = 5; private const int maxHits = 5;
public Animator dogAnimator;
void Start() void Start()
{ {
// Make sure fractured crate and crack pieces are hidden at start
fracturedCrate.SetActive(false);
foreach (var piece in crackPiecesPerTap) 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() void Update()
{ {
if (Input.GetMouseButtonDown(0)) // Tap or click if (Input.GetMouseButtonDown(0))
{ {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit)) 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++; hitCount++;
if (hitCount < maxHits) if (hitCount < maxHits)
{ {
// Enable crack pieces one-by-one var piece = crackPiecesPerTap[index];
if (hitCount - 1 < crackPiecesPerTap.Length)
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) if (crackSound && audioSource)
@ -68,23 +82,37 @@
private void BreakCrate() private void BreakCrate()
{ {
wholeCrate.enabled = false; foreach (var piece in crackPiecesPerTap)
boxCollider.enabled = false; {
fracturedCrate.SetActive(true); if (piece != null)
{
var rb = piece.GetComponent<Rigidbody>();
if (rb && rb.isKinematic)
{
rb.isKinematic = false;
rb.AddExplosionForce(300f, transform.position, 2f);
}
}
}
if (crashSound && audioSource) if (crashSound && audioSource)
audioSource.PlayOneShot(crashSound); 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); gameObject.SetActive(false);
} }
[ContextMenu("Test")]
[ContextMenu("Test Hit Index 0")]
public void Test() public void Test()
{ {
RegisterHit(); RegisterHit(0);
} }
} }
} }

File diff suppressed because it is too large Load Diff