What is the best way of breaking boxes? Colliders , Raycasts ? I am using colliders right now and it works awfully but with raycasts i will only be able to break one at the time or ?
how it works right now is that the player has a wrench with a collider and the script and when i press left-click it plays the animation (Separate script) and if the box is inside the collider while it attacks it breaks. But it breaks the boxes wrong it sometimes breaks the ones that are behind another one first sometimes it wont even break the box.
this is how i have the code right now:
var hasAttacked : boolean;
var ExplodeScript : Blink;
var explosionPrefab : GameObject;
var Bolts : GameObject;
var Nanotech : GameObject;
var Ammo : GameObject[];
var RandomAmmo : int;
var woodPrefab : GameObject;
var Offset : Vector3;
var Projectile : GameObject;
function Update () {
Attack();
RandomAmmo = Random.Range(0,10);
}
function Attack(){
if(Input.GetMouseButtonDown(0))
{
attacking = true;
if(this.tag == "Gun"){
var pel = Instantiate(Projectile, transform.position, transform.rotation);
pel.rigidbody.AddForce(transform.forward * 800);
}
yield WaitForSeconds(0.5f);
attacking = false;
}
}
function OnTriggerEnter(col : Collider)
{
if(col.tag == "Box" && attacking == true && hasAttacked == false)
{
ExplodeScript = col.gameObject.GetComponent("Blink");
yield WaitForSeconds(0.1f);
col.rigidbody.AddForce(gameObject.Find("Third Person Character").transform.forward * 250);
hasAttacked = true;
yield WaitForSeconds(0.5f);
hasAttacked = false;
}
else if(col.tag == "BoltBox" && attacking == true && hasAttacked == false)
{
ExplodeScript = col.gameObject.GetComponent("Blink");
yield WaitForSeconds(0.1f);
col.rigidbody.AddForce(gameObject.Find("Third Person Character").transform.forward * 250);
hasAttacked = true;
Instantiate(woodPrefab, col.transform.position, col.transform.rotation);
Instantiate(Bolts, col.transform.position, col.transform.rotation);
Destroy(col.gameObject);
yield WaitForSeconds(0.5f);
hasAttacked = false;
}
else if(col.tag == "NanoBox" && attacking == true && hasAttacked == false)
{
ExplodeScript = col.gameObject.GetComponent("Blink");
yield WaitForSeconds(0.1f);
col.rigidbody.AddForce(gameObject.Find("Third Person Character").transform.forward * 250);
hasAttacked = true;
Instantiate(woodPrefab, col.transform.position, col.transform.rotation);
Instantiate(Nanotech, col.transform.position + Offset, col.transform.rotation);
Destroy(col.gameObject);
yield WaitForSeconds(0.5f);
hasAttacked = false;
}
else if(col.tag == "AmmoBox" && attacking == true && hasAttacked == false)
{
ExplodeScript = col.gameObject.GetComponent("Blink");
yield WaitForSeconds(0.1f);
col.rigidbody.AddForce(gameObject.Find("Third Person Character").transform.forward * 250);
hasAttacked = true;
Instantiate(woodPrefab, col.transform.position, col.transform.rotation);
Instantiate(Ammo[RandomAmmo], col.transform.position, col.transform.rotation);
Destroy(col.gameObject);
yield WaitForSeconds(0.5f);
hasAttacked = false;
}
else if(col.collider.tag == "ExplosiveBox" && attacking == true)
{
ExplodeScript = col.gameObject.GetComponent("Blink");
yield WaitForSeconds(0.1f);
Instantiate(explosionPrefab, col.transform.position, col.transform.rotation);
col.gameObject.active = false;
Destroy(col.gameObject);
ExplodeScript.Explode();
hasAttacked = true;
yield WaitForSeconds(0.5f);
hasAttacked = false;
}
}
function OnTriggerExit(col : Collider)
{
if(col.tag == "Box")
{
hasAttacked = false;
}
if(col.tag == "BoltBox")
{
hasAttacked = false;
}
if(col.tag == "AmmoBox")
{
hasAttacked = false;
}
else if(col.collider.tag == "ExplosiveBox")
{
hasAttacked = false;
}
}