I have a hard time understanding how to combine meshes in unity by scripting. I am working with C# as javascript is a bit confusing for me. As an example, lets say i have two plane mesh, planeA and planeB. In update these two mesh will be combined, the combined mesh will then be assigned to planeA, and planeB will be destroyed. Next update, a new plane mesh, planeC is spawned and the previous process will be repeated. So planeA will have a growing mesh over the time. I have tried following the scripting API, but at the next update, the combined mesh is not assigned to planeA, it will become empty. Any tutorial or script that I found from the internet, from my point of understanding, combines meshes and assign it to a new gameObject, which is different to what I am trying to do. Please help me as this issue has been bugging me for quite a while and I cannot progress my project with this issue unsolved.
I made a capsule follow a small sphere using navmesh. Every 1 second, a circle will spawn at the current position of the capsule. I want to combine these circle meshes into one gameobject, where the position of the gameobject is the capsule's starting position. Code (CSharp): using UnityEngine; using UnityEditor; using System.Collections; [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class Traverser2 : MonoBehaviour { public float masa; public Transform Region; public GameObject traversedRegion, currentRegion; void Start () { traversedRegion = new GameObject(); traversedRegion.AddComponent<MeshFilter>().mesh = Region.GetComponent<MeshFilter>().sharedMesh; traversedRegion.AddComponent<MeshRenderer>(); traversedRegion.transform.position = transform.position; traversedRegion.transform.localScale = Region.transform.localScale; } // Update is called once per frame void Update () { masa += Time.deltaTime; if (masa >= 1f){ currentRegion = new GameObject(); currentRegion.AddComponent<MeshFilter>().mesh = Region.GetComponent<MeshFilter>().sharedMesh; currentRegion.AddComponent<MeshRenderer>(); currentRegion.transform.position = transform.position; currentRegion.transform.localScale = Region.transform.localScale; CombineInstance[] combine = new CombineInstance[2]; combine[0].mesh = traversedRegion.GetComponent<MeshFilter>().sharedMesh; combine[0].transform = traversedRegion.GetComponent<MeshFilter>().transform.localToWorldMatrix; combine[1].mesh = currentRegion.GetComponent<MeshFilter>().sharedMesh; combine[1].transform = currentRegion.GetComponent<MeshFilter>().transform.localToWorldMatrix; traversedRegion.GetComponent<MeshFilter>().mesh = new Mesh(); traversedRegion.GetComponent<MeshFilter>().mesh.CombineMeshes(combine); Destroy(currentRegion); masa = 0f; } } }
You have quite some stuff going on in this script already, especially if you consider that it doesn't work appropriately. Did you try to minimize the functionality and to make sure it works step by step. You may e.g. start with combining two given meshes and as soon as this works, you go on. There are a few suspicious parts in your code. E.g. I don't understand why you are using transform.position at some points or why you create a new mesh at line 42 each time. Those may be totally valid though.
I want to create something like a memory for the gameobject. What I mean by this is that I want the capsule to 'remember' where it has gone before. That is why every one second, the code will create a new mesh at its' current position. After several seconds, I expect the combined circle mesh to be like a pathway showing where the capsule had been. I don't really know how to check if the combined mesh is created. If I used debug.log, it will only print combineInstance. Can you provide me any tips regarding this.
I would start simpler. Just create a new scene with two meshes in it and then combine them on start/awake and verify it each time. If your code doesn't work now, you need to make one step back.
I tried to run my code a few times and after a while i noticed something. Before this, I view the scene tab to only focus on the part where the capsule is, so this is what I saw after zooming out, a lot. I'm assuming this is caused by the part 'transform.localtoworldmatrix'. That is the only part that I could not understand from the example code and the scripting help does not provide much help, and since my code is generating new mesh every second, calling that piece of code every time, it somehow caused the generated mesh to enlarge. Can someone help in explaining it (with some simple example if possible).