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 ephil12 · May 14, 2014 at 02:50 PM · collisionraycastdestroy

Destroying all objects with a certain tag that ray cast if colliding with

I'm trying to destroy cubes that are touching each other. Each cube cast a ray that only extends long enough to collide with the next cube. I want to be able to destroy multiple cubes that are on top or next to each other that are the same when they are struck by an object. I tried to use a null stament so that when one cube is destroyed and it was touching another cube it would cause a chain reaction and they would be destroyed as well, but it did not work. This is my ray cast code so far.

     public Rigidbody cube;
     public GameObject cubeAray;
     public float distanceUp=.23f;
     public float distanceLeft=.95f;
     public float distanceDown=.25f;
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         RaycastHit hit;
 
         Vector3 fwd = cube.transform.TransformDirection(Vector3.forward);
         Vector3 uwd= cube.transform.TransformDirection (Vector3.up);
         Vector3 dwn=cube.transform.TransformDirection (Vector3.down);
 
         Debug.DrawRay(cube.transform.position+Vector3.left*distanceLeft, fwd * .4f, Color.green);
         Debug.DrawRay(cube.transform.position+Vector3.left*distanceUp+Vector3.up*distanceUp, uwd* .3f, Color.green);
         Debug.DrawRay(cube.transform.position+Vector3.left*distanceDown+Vector3.down*distanceDown, dwn* .3f, Color.green);
 
         //raycast hit UP
         if(Physics.Raycast (cube.transform.position+Vector3.left*distanceUp+Vector3.up*distanceUp, uwd, out hit, .3f))
         {
             {
                     print ("UP UP");
 
             }
         }
 
         //raycast hit left
         if(Physics.Raycast (cube.transform.position+Vector3.left*distanceLeft, fwd, out hit, .4f))
         {
 
             if(hit.collider.tag=="PlayerCube" )
             {
                 print ("SIDE SIDE");
 
             }
         }
 
         //raycast hit down
         if(Physics.Raycast (cube.transform.position+Vector3.left*distanceDown+Vector3.down*distanceDown, dwn, out hit, .3f))
         {
                 if(hit.collider.tag=="PlayerCube" )
             {
                     print ("UP UP");
             }
         }
     }
 }
 
Comment
Add comment · Show 5
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 Sisso · May 14, 2014 at 02:55 PM 0
Share

What didn't work? The rays are wrong? Didn't collide? It collide with anything other that PlayerCube?

avatar image robertbu · May 14, 2014 at 03:13 PM 0
Share

You can approach this in one of three ways:

  • Put a script similar to this one on every cube and have them check themselves.

  • Get a list of all the cubes and iterate through them running this same code

  • Use recursion. One you get one hit, you then apply this same code to the hit object as well. Note you'll want to change the tag to avoid looping.

avatar image ephil12 · May 15, 2014 at 10:29 PM 0
Share

I need all the objects that the raycast is touching to be destroyed when an object collides with one of the cubes and destroys it.

avatar image robertbu · May 16, 2014 at 01:19 AM 0
Share

You are looking for a chain reaction. That is if one block is destroyed then any touching neighbors of that block are also destroyed. And Raycasting is how you detect the neighbors. Correct? If so, then the Recursion approach is best.

Pseudo code:

 void CheckNeightborsAndDestory(Transform target) {
      target.tag = "ToBeDestoryed";
      if (Raycast left gets a hit) {
          CheckNeightborsAndDestory(hit.transform);
      }
      if (Raycast right tets a hit) {
          CheckNeighborsAndDestory(hit.transform);
      }
      if (Rayast up gets a hit) {
          CheckNeightorsAndDestory(hit.transform);
      }
      if (Raycast down gets a hit) {
          CheckNeightborsAndDestory(hit.transform);
      }
      Destory(target.gameObject);
 }
avatar image ephil12 · May 19, 2014 at 08:32 PM 0
Share

it comes out with stack overflow

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Destroy Turret with machine Gun 0 Answers

Trouble with raycast hitting a tagged object 0 Answers

Destroying a prefab with a prefab? 2 Answers

How to check if a raycasthit detects an angle less than 45 degrees and touches the ground 2 Answers

Subframe Collision Detection 0 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