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 sorasnobody · Apr 21, 2015 at 02:35 PM · mathproceduralprocedural mesh

Struggling with procedural mesh math

Hello! I've been struggling for the last few days to make a procedurally generated road. The road script has a function that makes it turn at a given angle, and a function that lets it go forward at the given angle. My problem arises when trying to make a right turn after having already moved forward. I can't for the life of me figure out the issue, but I must be messing up the x and y coordinates somewhere.

alt text

 using UnityEngine;
 using System.Collections;
 
 
     public class procPlane : MonoBehaviour {
     
         public Mesh mesh;
         public Vector3[] vertices;
         public int currentVertice;
         public Vector3[] normales;
         public Vector2[] uvs;
         public int[] triangles;
         public int currentTriangle;
         
         //current vertice number
         public int verticeNumber = 4;
     
         //current x and z outer values.

             //values for the outer vertice
         public float x_out = -0.5f;
         public float z_out = 0.1f;
     
         public float angle = 0.0f;

             //values for the inner vertice

         private float x_in = 0.5f;
         public float z_in = 0.1f;
     
         public float xi=0.5f, xo=-0.1f, zi=0.1f, zo=0.1f;
     
         // Use this for initialization
         void Start () {
     
             
             MeshFilter filter = gameObject.AddComponent< MeshFilter >();
             mesh = filter.mesh;
             mesh.Clear();
     
             #region Vertices        
             vertices = new Vector3[1000];
             currentVertice = 0;
     
             vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, -0.1f);
             vertices[currentVertice++] = new Vector3(0.5f, 0.0f, -0.1f);
             vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.0f);
             vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.0f);
             vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.1f);
             vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.1f);
             #endregion
             
             #region Normales
              normales = new Vector3[ vertices.Length ];
             for( int n = 0; n < normales.Length; n++ )
                 normales[n] = Vector3.up;
             #endregion
             
             #region Triangles
     
             triangles = new int[2*6*100];
             currentTriangle = 0;
             triangles[currentTriangle++] = 2;
             triangles[currentTriangle++] = 1;
             triangles[currentTriangle++] = 0;
             triangles[currentTriangle++] = 2;
             triangles[currentTriangle++] = 3;
             triangles[currentTriangle++] = 1;
     
             triangles[currentTriangle++] = 4;
             triangles[currentTriangle++] = 3;
             triangles[currentTriangle++] = 2;
             triangles[currentTriangle++] = 4;
             triangles[currentTriangle++] = 5;
             triangles[currentTriangle++] = 3;
     
             #endregion
             
             mesh.vertices = vertices;
             mesh.normals = normales;
             mesh.uv = uvs;
             mesh.triangles = triangles;
             
             mesh.RecalculateBounds();
             mesh.Optimize();
         }
         
         // Update is called once per frame
         void Update () {
             if(Input.GetKeyDown(KeyCode.UpArrow))
             {
   

                             //Translating vertices forward at given angle
                 Vector3 A = Quaternion.Euler(0,angle,0)*Vector3.forward*0.1f;
     
                 x_out+=A.x;
                 x_in+=A.x;
                 z_in += A.z;
                 z_out += A.z;
                 Debug.LogWarning(z_in);
                 xo += A.x;
                 xi += A.x;
     
                 zo += A.z;
                 zi += A.z;
     
                 vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
                 vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
     
                 //normals
     
                     normales[currentVertice] = Vector3.up;
     
                 //triangles
                 verticeNumber += 2; 
                 triangles[currentTriangle++] = verticeNumber;
                 triangles[currentTriangle++] = verticeNumber-1;
                 triangles[currentTriangle++] = verticeNumber-2;
                 triangles[currentTriangle++] = verticeNumber;
                 triangles[currentTriangle++] = verticeNumber+1;
                 triangles[currentTriangle++] = verticeNumber-1;
     
                 mesh.vertices = vertices;
                 mesh.normals = normales;
                 mesh.uv = uvs;
                 mesh.triangles = triangles;
                 
                 mesh.RecalculateBounds();
                 mesh.Optimize();
             }
             if(Input.GetKeyDown(KeyCode.RightArrow))
             {
     
                 //z_out += z_out;
                 //z_in += z_in;
                 angle += 9.0f;
 

                  //calculating x and y positions on the outer edge of the bend.
                 float x_outer = (float)-(2.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
                 float z_outer = (float)(2.0f * Mathf.Sin(angle*Mathf.PI/180.0f));
     
                 x_out =  x_outer;
     
     
                 z_out = zo + z_outer;
     
                  //calculating x and y positions on the inner edge of the bend.
                 float x_inner = (float)-(1.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
                 float z_inner = (float)(1.0f * Mathf.Sin(angle*Mathf.PI/180.0f))-0.1f;
     
                 x_in = x_inner;
                 z_in = zi + z_inner;
                 Debug.LogWarning(zi);
                 Debug.LogWarning(z_inner);

                 //outer vertice
                 vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);

                 //inner vertice
                 vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
                 
                 //normals
                 normales[currentVertice] = Vector3.up;
     
                 //triangles
                 verticeNumber += 2;
                 triangles[currentTriangle++] = verticeNumber;
                 triangles[currentTriangle++] = verticeNumber-1;
                 triangles[currentTriangle++] = verticeNumber-2;
                 triangles[currentTriangle++] = verticeNumber;
                 triangles[currentTriangle++] = verticeNumber+1;
                 triangles[currentTriangle++] = verticeNumber-1;
                 
                 mesh.vertices = vertices;
                 mesh.normals = normales;
                 mesh.uv = uvs;
                 mesh.triangles = triangles;
                 
                 mesh.RecalculateBounds();
                 mesh.Optimize();
             }
         }
     }


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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Adding extra materials to a mesh programmatically 0 Answers

Making a Procedural Cube Mesh 1 Answer

C# Proceducal Mesh terrain 2 Answers

Sphere made of cubes algorithm 4 Answers

How to avoid Memory allocation on procedural Mesh generation 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