Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alexander11 · Aug 11, 2016 at 03:31 AM · c#unity 5mesh3dmanipulate

How do i have a Vector3[] position be in another Vector3[] position (That Updates)??

Hello i am having some trouble linking the vertices(Vector3[]) to the RedSpheres(Vector3) so i can manipulate the vertices,

to see what i am talking about see Pic1.

And in Pic2 are the results i am getting, does anyone know how to fix this?

-Pic1 (What i am trying to achieve) alt text

-Pic2 (The results i am getting when i move the Cube and its Spheres) alt text

And here is the code so you know what i have done.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class ProceduralRoad : MonoBehaviour
 {
 
     private Vector3[] cubePoints;
     private Vector3[] spherePoints;
 
     public GameObject cubePrefab;
     public GameObject spherePrefab;
 
     private Vector3[] vertices;
     private Mesh mesh;
 
 
     public int cubeAmount, sphereAmount, offset;
     //private int xSize, zSize;
     private Vector3 gizmoCubeSize = new Vector3(0.1f, 0.1f, 0.1f);
     void Start()
     { 
 
 
         //xSize = cubeAmount;
         //zSize = sphereAmount;
 
         cubePoints = new Vector3[(cubeAmount + 1)];
         for (int i = 0, x = 0; x <= cubeAmount; i++, x++)
         {
             cubePoints[i] = new Vector3(x * offset, 0);
             // need the cube for parenting spheres, keep it
             var cube = Instantiate(cubePrefab, cubePoints[i], Quaternion.identity) as GameObject;
             spherePoints = new Vector3[(sphereAmount + 1)];
             cube.transform.SetParent(this.transform);
 
             for (int j = 0, z = 0; z <= sphereAmount; j++, z++)
             {
                 spherePoints[j] = new Vector3(0, 0, z + 1); // +1 because otherwise the first sphere is within the parent cube
                                                             // You might want to track the sphere instead of the vec3?
                 var sphere = Instantiate(spherePrefab, Vector3.zero, Quaternion.identity) as GameObject;
                 // parent
                 sphere.transform.SetParent(cube.transform);
                 // do positioning relative to parent
                 sphere.transform.localPosition = spherePoints[j]; ;
             }
         }
     }
     void Update()
     {
         Generate();
     }
 
     private void Generate()
     {
         
         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
         mesh.name = "Procedural Road";
 
 
         vertices = new Vector3[(cubeAmount + 1) * (sphereAmount + 1)];
         Vector2[] uv = new Vector2[vertices.Length];
 
         //I'm pretty sure this does nothing.
         for (int i = 0; i < spherePoints.Length; i++)
             vertices[i] = spherePoints[i];
 
         for (int i = 0, y = 0; y <= sphereAmount; y++)
         {
             for (int x = 0; x <= cubeAmount; x++, i++)
             {
                 vertices[i] = new Vector3(x * offset, 0, y + 1);
                 uv[i] = new Vector2((float)x / cubeAmount, (float)y / sphereAmount);
             }
         }
         int[] triangles = new int[cubeAmount * sphereAmount * 6];
         for (int ti = 0, vi = 0, y = 0; y < sphereAmount; y++, vi++)
         {
             for (int x = 0; x < cubeAmount; x++, ti += 6, vi++)
             {
                 triangles[ti] = vi;
                 triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                 triangles[ti + 4] = triangles[ti + 1] = vi + cubeAmount + 1;
                 triangles[ti + 5] = vi + cubeAmount + 2;
             }
         }
     
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.uv = uv;
         mesh.triangles = triangles;
     }
     private void OnDrawGizmos()
     {
         if (vertices == null)
             return;
         Gizmos.color = Color.black;
         for (int i = 0; i < vertices.Length; i++)
         {
             Gizmos.DrawSphere(vertices[i], 0.1f);
         }
         Gizmos.color = Color.green;
         for (int i = 0; i < cubePoints.Length; i++)
         {
             Gizmos.DrawCube(cubePoints[i], gizmoCubeSize);
         }
     }
 }


@Fattie

capture13.png (78.3 kB)
capture14.png (71.7 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
Best Answer

Answer by Bunny83 · Aug 11, 2016 at 12:16 PM

Change:

 private Vector3[] spherePoints;

into

 private Transform[] spherePoints;

Instead of storing the initial position into the array you would store the actual instantiated object into that array. When assigning the positions to the vertices you would need to use ".position" of each point.

Furthermore the positions of each sphere is in worldspace. If the mesh parent object is not located at "0,0,0" you would need to use InverseTransformPoint of the mesh parent to convert the world position into a localspace position of the mesh. If you parent your spheres to the mesh you can actually use ".localPosition" directly.

In your line 68 - 75 you again overwrite the content of your vertices which doesn't make much sense.

You probably only want:

 for (int i = 0; i < spherePoints.Length; i++)
     vertices[i] = transform.InverseTransformPoint(spherePoints[i].position);

You also want to create all those array once outside of your "Generate" method and reuse them each time you want to update the mesh. Since your uv array never changes you could create that in Start as well.

edit

You probably want something like this:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class ProceduralRoad : MonoBehaviour
 {
     private Transform[] cubes;
     private Transform[] spheres;
 
     public GameObject cubePrefab;
     public GameObject spherePrefab;
 
     private Vector3[] vertices;
     private Mesh mesh;
 
     public int xSize = 5;
     public int zSize = 5;
     public float offset = 1f;
 
     void Start()
     {
         cubes = new Transform[xSize];
         spheres = new Transform[xSize * zSize];
 
         vertices = new Vector3[spheres.Length];
         Vector2[] uv = new Vector2[vertices.Length];
 
         for (int x = 0; x < xSize; x++)
         {
             var cube = (GameObject)Instantiate(cubePrefab, new Vector3(x * offset, 0), Quaternion.identity);
             cubes[x] = cube.transform;
             cube.transform.SetParent(this.transform);
 
             for (int z = 0; z < zSize; z++)
             {
                 var sphere = (GameObject)Instantiate(spherePrefab, Vector3.zero, Quaternion.identity);
                 spheres[z + x * zSize] = sphere.transform;
                 sphere.transform.SetParent(cube.transform);
                 sphere.transform.localPosition = new Vector3(0, 0, z + 1);
                 uv[z + x * zSize] = new Vector2((float)x / (xSize - 1), (float)z / (zSize - 1));
             }
         }
         GetComponent<MeshFilter>().sharedMesh = mesh = new Mesh();
         mesh.name = "Procedural Road";
         int[] triangles = new int[(xSize-1)*(zSize-1) * 6];
         for (int ti = 0, z = 0; z < zSize-1; z++)
         {
             for (int x = 0; x < xSize-1; x++, ti += 6)
             {
                 int vi = x * zSize + z;
                 triangles[ti    ] = vi;
                 triangles[ti + 1] = vi + 1;
                 triangles[ti + 2] = vi + zSize;
 
                 triangles[ti + 3] = vi + zSize;
                 triangles[ti + 4] = vi + 1;
                 triangles[ti + 5] = vi + zSize + 1;
             }
         }
         mesh.vertices = vertices;
         mesh.uv = uv;
         mesh.triangles = triangles;
         UpdateVertices();
     }
 
     void Update()
     {
         UpdateVertices();
     }
 
     private void UpdateVertices()
     {
         for (int i = 0; i < spheres.Length; i++)
             vertices[i] = transform.InverseTransformPoint(spheres[i].position);
         mesh.vertices = vertices;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
     }
 }

Comment
Add comment · Show 2 · 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 alexander11 · Aug 11, 2016 at 09:44 PM 0
Share

I am getting ERROR error CS0029: Cannot implicitly convert type UnityEngine.Transform' to UnityEngine.Vector3' with spherePoints = new Vector3[(sphereAmount + 1)];(line 34) after i changed Vector3[] spherePoints to Transform[] spherePoints, would you know how to fix this?

avatar image alexander11 · Aug 11, 2016 at 11:33 PM 0
Share

Thanks a lot, i did not know you could do "cubes = new Transform[zSize];" i thought you could only do that with Vector3's.

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

222 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 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

How do i modify a mesh Filter(C#)? 2 Answers

How can I generate a up facing quad mesh with adjustable res? 0 Answers

How do i create a Mesh in the middle of a Catmull spline? 0 Answers

How do i replace a Mesh Filter?? 1 Answer

Getting errors with extrusion script. 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