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
1
Question by iamthecoolguy11 · Dec 31, 2014 at 07:17 AM · c#voxeltrianglescalculations

How do I calculate this voxel script so I don't get a out of range error?

I made this voxel script from scratch and i'm still new to voxel but I made this on my own so im guessing its just a calculation isshu but iv tried a lot of different things and its still not working correctly it just gets this error on play. alt text

This is my code

 sing UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshCollider))]
 public class RealVoxel : MonoBehaviour {
     public int length = 1;
     public int width = 1; 
     public MeshRenderer meshRender;
     public MeshFilter meshFilter;
     public MeshCollider meshCollider; 
     public List<int>tris = new List<int>();
     public List<Vector2>uvs = new List<Vector2>();
     public List<Vector3>verts = new List<Vector3>();
     public int block = 0; 
     public int tri = 0; 
 
     void Start()
     {
         meshRender = this.GetComponent<MeshRenderer>(); 
         meshFilter = this.GetComponent<MeshFilter>();
         meshCollider = this.GetComponent<MeshCollider>();
         genCube(); 
         EditTerrain(); 
     }
     void EditTerrain()
     {
         Mesh mesh= new Mesh(); 
 
         Vector3 pos = new Vector3(0,1,1);
 
         for(int i = 0; i < verts.Count; i++)
         {
             if(verts[i] == pos)
             {
                 block = i; 
                 break; 
             }
         }
         tri = block * 6; 
 
         verts.Remove(verts[block]); 
         verts.Remove(verts[block + 1]);
         verts.Remove(verts[block + 2]);
         verts.Remove(verts[block + 3]);
 
         uvs.Remove(uvs[block]); 
         uvs.Remove(uvs[block+1]); 
         uvs.Remove(uvs[block+2]); 
         uvs.Remove(uvs[block+3]); 
 
         tris.Remove(tris[tri]); 
         tris.Remove(tris[tri+1]); 
         tris.Remove(tris[tri+2]); 
 
         tris.Remove(tris[tri+3]); 
         tris.Remove(tris[tri+4]); 
         tris.Remove(tris[tri+5]);
 
         Recalcutlate(mesh); 
     }
     void genCube()
     {
         for(int x = 0; x <= length; x++)
         {
             top(new Vector3(x,1,0));
 
                 for(int z = 0; z <= width; z++)
                 {
                     top(new Vector3(x,1,z)); 
                 }
         }
     }
     public virtual void removeBlock(Vector3 corner)
     {
         Mesh visualMesh = new Mesh();
     }
 
     public virtual void top(Vector3 corner)
     {
         Mesh visualMesh = new Mesh();
 
         int index = verts.Count;
 
         verts.Add(corner);
         verts.Add(corner + new Vector3(0,0,1));
         verts.Add(corner + new Vector3(1,0,0) + new Vector3(0,0,1));
         verts.Add(corner + new Vector3(1,0,0));
 
         uvs.Add(new Vector2(0,0));
         uvs.Add(new Vector2(0,1));
         uvs.Add(new Vector2(1,1));
         uvs.Add(new Vector2(1,0));
 
         tris.Add(index + 0);
         tris.Add(index + 1);
         tris.Add(index + 2);
 
         tris.Add(index + 2);
         tris.Add(index + 3);
         tris.Add(index + 0);
 
         Recalcutlate(visualMesh); 
     }
     void Recalcutlate(Mesh visualMesh)
     {
         visualMesh.vertices = verts.ToArray();
         visualMesh.uv = uvs.ToArray();
         visualMesh.triangles = tris.ToArray();
         visualMesh.RecalculateBounds();
         visualMesh.RecalculateNormals();
         
         meshFilter.mesh = visualMesh;
         meshCollider.sharedMesh = visualMesh; 
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by iamthecoolguy11 · Dec 31, 2014 at 07:42 AM

ok so I figured it out I was trying to remove things from the list instead of changing them so all I need to do is change this

 verts[block] = new Vector3(0,0,0); 
         verts[block+1] = new Vector3(0,0,0); 
         verts[block+2] = new Vector3(0,0,0); 
         verts[block+3] = new Vector3(0,0,0); 
 
         uvs[block] = new Vector3(0,0,0);
         uvs[block+1] = new Vector3(0,0,0);
         uvs[block+2] = new Vector3(0,0,0);
         uvs[block+3] = new Vector3(0,0,0);
 
         tris[tri] = 0; 
         tris[tri+1] = 0; 
         tris[tri+2] = 0; 
 
         tris[tri+3] = 0; 
         tris[tri+4] = 0; 
         tris[tri+5] = 0; 
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
avatar image
1

Answer by Eluate · Dec 31, 2014 at 07:24 AM

Replace:

    for(int i = 0; i < verts.Count; i++)
          {
              if(verts[i] == pos)
              {
                  block = i; 
                  break; 
              }
          }

with

    for(int i = 0; i < verts.Count - 1; i++)
          {
              if(verts[i] == pos)
              {
                  block = i; 
                  break; 
              }
          }


Remember, arrays start with an index of 0. Count counts them and starts with 1 instead of zero.

Comment
Add comment · Show 1 · 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
avatar image iamthecoolguy11 · Dec 31, 2014 at 07:40 AM 0
Share

this also helped fix it :)

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How come do I keep getting this error on my voxel script 1 Answer

Collider Performance Question 1 Answer

What is a simple way to make a voxel face? 1 Answer


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