- Home /
What is the best way of breaking boxes
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;
}
}
What IS your wanted behavior? You mentioned breaking only the first one hit by a raycast, but do you want it to break only the first one or more than that? There are different approaches for each wanted effect, but Raycast should be able to deal with both.
Answer by Andres-Fernandez · May 08, 2014 at 01:42 PM
I'm not sure if it's the cause of all the problems, but you have a couple yields that might be causing some unwanted behaviour. Yield makes a function return to the following instruction the next time the function is called. If you want to control time, I suggest you use coroutines.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
what is the best way to detect if an object has been clicked on? 1 Answer
raycasthit questions 2 Answers
Remove object with specific object 1 Answer
Accessing colliders, in if statement 2 Answers