• 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 /
  • Help Room /
avatar image
0
Question by AWicke21 · Oct 20, 2018 at 03:37 PM · unityeditor

Using coliders in a procedural voxel space

Long story short I am making a 3d array of "objects" and then in AR I am removing them/swapping them (architecture project). However at the start there is a large chunk of them I wish to remove as they are in conflict with other objects (parts in green). For the life of me I cant seem to get the mesh colliders or even a rotated box collider to work as in picture. If anyone has any suggestions that would be great! I am using void start() to make the 3d array of them. The box is child to an empty and that empty is rotated, not the child with the collider. When putting the mesh colliders on the green parts (modeled objects) the collisions seem to be happening in an overall volume, kinda like the Convex but an overall rectilinear volume.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.IO;
 using System.Runtime;
 
 public class cubeArray : MonoBehaviour
 {
 
     //for the array
     public int length = 3;
     public int width = 3;
     public int height = 3;
     public GameObject voxelObjectOpen;
     public GameObject voxelObjectClosed;
     public GameObject Barge;
     GameObject[] voxels;
 
 
     Collider bargecollider;
     Collider voxelcollider;
 
     // public GameObject newVoxel;
     public GameObject imageT;
 
     bool SwapVoxelbool;
     bool HideVoxelbool;
 
     bool tru = true;
 
 
 
     //for the racasting
     Ray moRay;
     Transform vxTransform;
     public LayerMask VoxelObjects;
     RaycastHit vxHit;
 
 
     void Start()
     {
         bargecollider = Barge.GetComponent<MeshCollider>();
         int i = 0;
         voxels = new GameObject[width * length * height];
         for (int y = 0; y < height - 1; y++)
         {
             for (int z = 0; z < length - 1; z++)
             {
                 for (int x = 0; x < width - 1; x++)
                 {
                     voxels[i] = (GameObject)GameObject.Instantiate(voxelObjectClosed, new Vector3(x - 3.5f, y + 0.5f, z - 25), Quaternion.identity);
                     voxels[i].layer = 8;
                     voxels[i].transform.SetParent(imageT.transform, true);
                     voxels[i].name = "closed-voxel_ID" + i;
                     voxels[i].transform.GetComponent<VoxelParameters>().VoxelType = "closed-voxel";
                     voxelcollider = voxels[i].GetComponent<Collider>();
                     if (bargecollider.bounds.Intersects(voxelcollider.bounds))
                     {
                         Destroy(voxels[i].gameObject);
                     }
 
 
                     i++;
                 }
             }
         }
 
         vxHit = new RaycastHit();
     }
 
     void Update()
     {
         moRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Input.GetMouseButtonDown(0))
 
 
             if (SwapVoxelbool)
             {
                 SwapVoxel();
             }
 
             else
             {
                 TurnoffVoxel();
             }
     }
 
 
     public void SavePositions()
     {
         SaveInventory();
     }
 
 
     public void SwapVoxels(bool swap)
     {
         SwapVoxelbool = swap;
     }
 
     void SwapVoxel()
     {
         if (Physics.Raycast(moRay, out vxHit, Mathf.Infinity, VoxelObjects))
         {
             vxTransform = vxHit.transform;
 
             VoxelParameters param = vxTransform.GetComponent<VoxelParameters>();
             if (param.VoxelType == "closed-voxel")
             {
                 GameObject newvoxel = GameObject.Instantiate(voxelObjectOpen, vxTransform.transform.position, Quaternion.identity);
                 newvoxel.transform.eulerAngles = imageT.transform.eulerAngles;
                 int index = System.Array.IndexOf(voxels, vxTransform.gameObject);
                 Destroy(vxTransform.gameObject);
                 voxels[index] = newvoxel;
                 string voxeltype = "open-voxel";
                 VoxelNaming(newvoxel, voxeltype, index);
             }
             if (param.VoxelType == "open-voxel")
             {
                 GameObject newvoxel = GameObject.Instantiate(voxelObjectClosed, vxTransform.transform.position, Quaternion.identity);
                 int index = System.Array.IndexOf(voxels, vxTransform.gameObject);
                 newvoxel.transform.eulerAngles = imageT.transform.eulerAngles;
                 Destroy(vxTransform.gameObject);
                 voxels[index] = newvoxel;
                 string voxeltype = "closed-voxel";
                 VoxelNaming(newvoxel, voxeltype, index);
             }
         }
     }
 
 
 
     void VoxelNaming(GameObject voxel, string type, int index)
     {
         voxel.layer = 8;
         voxel.transform.SetParent(imageT.transform, true);
         voxel.name = type + "_ID:" + index;
         voxel.transform.GetComponent<VoxelParameters>().VoxelType = type;
     }
 
 
     void TurnoffVoxel()
     {
         if (Physics.Raycast(moRay, out vxHit, Mathf.Infinity, VoxelObjects))
         {
             vxTransform = vxHit.transform;
             vxTransform.gameObject.SetActive(false);
         }
     }
     public void CubeSpacing(float spacing)
     {
         int i = 0;
         for (int y = 0; y < width - 1; y++)
         {
             for (int z = 0; z < length - 1; z++)
             {
                 for (int x = 0; x < height - 1; x++)
                 {
                     VoxelParameters param = voxels[i].GetComponent<VoxelParameters>();
                     voxels[i].transform.position = new Vector3(param.XLocation * spacing / 24, param.YLocation * spacing / 24, param.ZLocation * spacing / 24);
                     i++;
                     Debug.Log(i);
                 }
             }
         }
     }
 
     void SaveInventory()
     {
         string filePath = getPath();
         StreamWriter writer = new StreamWriter(filePath);
         writer.WriteLine("Type,Name,Voxeltype,X_Position,Y_Position,Z_Position,VoxelExsists");
         int i = 0;
         for (int y = 0; y < height - 1; y++)
         {
             for (int z = 0; z < length - 1; z++)
             {
                 for (int x = 0; x < width - 1; x++)
                 {
                     VoxelParameters param = voxels[i].GetComponent<VoxelParameters>();
                     writer.WriteLine(
                         voxels[i].GetType().ToString() + ","
                         + voxels[i].name + ","
                         + param.VoxelType + ","
                         + param.XLocation + ","
                         + param.YLocation + ","
                         + param.ZLocation + ","
                         + voxels[i].activeSelf);
                     i++;
                 }
             }
         }
         writer.Flush();
         writer.Close();
     }
 
     private string getPath()
     {
         return Application.dataPath + "/CSV/" + "VOXEL_LOCATIONS.csv";
     }
 }![alt text][1]


[1]: /storage/temp/126408-capture2.png

capture1.png (217.1 kB)
capture2.png (351.9 kB)
Comment
Add comment
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

155 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

APIs in unity 0 Answers

Unity hub help 0 Answers

Ar foundation Face Tracking 0 Answers

how to download open jdk,sdk, ndk recommended for unity Unity 2019.3.1, without unity hub. 1 Answer

I cant get the basic FollowPlayer script to go to Main Camera in the hierarchy 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