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 Nach0 · May 20, 2014 at 05:07 PM · c#meshmeshrendererroguelike

How can I make some tiles on a mesh higher up than others?

For the record, I'm using C#.

I'm completely lost with what I should do here - I've got code that can generate a plane mesh of arbitrary size, but I don't know where to go from here. I want to be able to make it so that some tiles (made of two triangles at this point) are higher than others. Given a multidimensional array of X,Z coordinates, with a binary flag for whether or not the tile is a wall, how can I make the code I have raise some tiles up in the air to form walls? I'm primarily experienced in making 2D games, and this is the first time I've ever attempted procedural generation in a 3D game, and one of the first 3D projects I've ever worked on.

I hope I'm making sense. If not, here's a glorious MS Paint illustration:

alt text

Just imagine you're looking at a cross section of a level from the side.

Below is the code I have:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Generate : MonoBehaviour {

 public int sizeX = 50;
 public int sizeZ = 50;
 public float tileSize = 1.0f;

 
 // Use this for initialization
 void Start () {
     BuildMesh();
 }
 


 public void BuildMesh() {
     int numTiles = sizeX * sizeZ;
     int numTris = numTiles * 2;
     List<int[]> map = new List<int[]>();

     //Create a map of all tile coordinates, with binary (0, 1) for whether or not the tile in question is a wall.
     for(int xi = 0; xi < sizeX; xi++){
         for(int zi = 0; zi < sizeZ; zi++){
             int[] xz = new int[3];
             xz[0] = xi;
             xz[1] = zi;
             xz[2] = Random.Range (0, 1);
             map.Add (xz);
             Debug.Log ("Tile " + xz[0] + ", " + xz[1] + " mapped!");
         }
     }
     
     int vSizeX = sizeX + 1;
     int vSizeZ = sizeZ + 1;
     int numVerts = vSizeX * vSizeZ;
     
     // Initialize the arrays that will hold our mesh data.
     Vector3[] vertices = new Vector3[numVerts];
     Vector3[] normals = new Vector3[numVerts];
     Vector2[] uv = new Vector2[numVerts];
     
     int[] triangles = new int[numTris * 3];
     
     int x, z;

     for(z=0; z < vSizeZ; z++) {
         for(x=0; x < vSizeX; x++) {

                 vertices[ z * vSizeX + x ] = new Vector3( x*tileSize, 0, -z*tileSize );
                 normals[ z * vSizeX + x ] = Vector3.up;
                 uv[ z * vSizeX + x ] = new Vector2( (float)x / sizeX, 1f - (float)z / sizeZ );
                 
                 
             
         }
     }
     Debug.Log ("Vertices done.");
     
     for(z=0; z < sizeZ; z++) {
         for(x=0; x < sizeX; x++) {
             int squareIndex = z * sizeX + x;
             int triOffset = squareIndex * 6;
             triangles[triOffset + 0] = z * vSizeX + x + 0;
             triangles[triOffset + 2] = z * vSizeX + x + vSizeX + 0;
             triangles[triOffset + 1] = z * vSizeX + x + vSizeX + 1;
             
             triangles[triOffset + 3] = z * vSizeX + x + 0;
             triangles[triOffset + 5] = z * vSizeX + x + vSizeX + 1;
             triangles[triOffset + 4] = z * vSizeX + x + 1;
         }
     }
     
     Debug.Log ("Triangles done.");
     
     // Now, we create a mesh and then use the data we just generated to populate it.
     Mesh mesh = new Mesh();
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.normals = normals;
     mesh.uv = uv;
     
     // Then we fill the mesh filter and collider with the mesh we just created.
     MeshFilter mesh_filter = GetComponent<MeshFilter>();
     MeshCollider mesh_collider = GetComponent<MeshCollider>();

     
     mesh_filter.mesh = mesh;
     mesh_collider.sharedMesh = mesh;
     Debug.Log ("Mesh Done.");
     

 }
 
 

}

untitled.png (21.1 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · May 20, 2014 at 06:35 PM

This line:

  vertices[ z * vSizeX + x ] = new Vector3( x*tileSize, 0, -z*tileSize );

is the one you should look at. It created a new Vector3 and adds this V3 to the array "vertices" which later gets used to build triangles and thus the mesh. So the new Vector3 is in effect the position of this specific vertex. The x and z positions should be self-explanatory, but look at the y positition: It is 0 for all vertices that are added in the for loop. You just change this y position from 0 to, let's say 1 for whichever vertices you want to be higher. Of course, you need to visualize how the vertices are arranged so that you know exactly which vertices need to be higher. Remember that each triangle is made up of three vertices, and each quad is made up of two triangles.

If you want to only make those vertices that are on a position flagged as a wall, you might have to add those vertices seperataly because otherwise you won't have 90 degree wall angles from the floor up, but rather slopes (You'll see what I mean when you keep the number of vertices).

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

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

How to active mesh renderer with a script C# 2 Answers

Filling area under positions with Mesh 0 Answers

How to Cut a Mesh - The Forest Style Tree Cutting 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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