Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by electfall70 · May 05, 2014 at 06:18 PM · raycastcolliderattackbox

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;
 }
 
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Swaggre · May 06, 2014 at 08:26 AM 1
Share

with a hammer I think

avatar image electfall70 · May 08, 2014 at 01:25 PM 0
Share

lol , i mean using raycasts or colliders

avatar image Paximillian · May 08, 2014 at 01:32 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges