I am doing some kind of an rts an I’m looking for a way to place objects.
Well what I am aiming for is: The player should be able to place trenches which can be extended, connected and upgraded.
I created an gameobject with a mesh filter, renderer and a script for instantiating objects. The spawning works so far. But for extending, connecting and so on I think I have to use the mesh filter of this object. But how can I get a mesh of an instantiated trench in the filter of this gameobject?
I read the manual for the meshfilter, I tried GetComponent but that doesn’t get me the mesh. After hours i’m out of ideas. Please help me as this is very import element of the game. When you think its the wrong way to use the mesh filter at all please tell me.
I modelled the trench with probuilder if this is important.
What do you mean by “it doesn’t get you the mesh”? A MeshFilter isn’t a Mesh, but it has a mesh, so you’d need to do:
var meshFilter = GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
//do stuff with mesh here
That said, I’m not sure that doing any kind of logic with the mesh is actually the right approach to what you’re trying to do (and procedural mesh alteration is notoriously tricky for a beginner). Typically, you’d have multiple prefabs for various kinds of connections, and spawn the different prefabs rather than attempting to modify the meshes.
If your goal with the mesh thing is to be able to dig into the terrain, I recommend looking into some of the voxel terrain packages on the Asset Store and finding one that that allows runtime modification of the terrain.
Hey I am sorry for the late reply. Unfortunately I don’t have much time for programming.
Its hard for me to describe the problem. My intention was the following:
I created this object just for the spawning of my trench. After spawning I wanted to alter the mesh and to achieve this I thought I have to get the mesh of the spawned trench into the filter of “trenchbuilder”
To string together prefabs sounds better. I will try this. But for snapping and aligning with the heights of the terrain don’t I have to work with the vertices? I looked for methods for runtime snapping that I gonna try.
But I think to let the trench follow the heights of the terrain I’m gonna have to use some kind of procedural mesh alteration.
Am i wrong at all? Sorry I’m a Beginner. The vertices approach is all I can think off.
Thanks for your help StarManta!
Ah, I see. If I understand correctly, you have your “trench” mesh (which includes the slopes/walls of the trench), and there are many such meshes for each possible combination of directions the trench can go. You need to adjust the heights of the vertices on these meshes so that the edges of the top of the trench line up with the terrain. Is that correct?
I’ll assume yes. If your mesh’s y=0 height is “ground level”, then you should be able to loop through the vertices and just add the terrain height to the vertex - as far as mesh modification goes this is pretty straightforward, so you’ll just have to worry about the “boilerplate” mesh stuff. Since it’s mostly boilerplate code, this one is probably easier for me to just write some code than try to explain every piece of logic as I normally would.
public void AlterHeightsOfMesh(GameObject go, Terrain t) {
var meshFilter = go.GetComponent<MeshFilter>();
var mesh = meshFilter.mesh; //note: this makes a copy of the mesh, which is what we want
Vector3[] vertices = mesh.vertices;
for (int v=0;v<vertices.Length;v++) {
Vector3 thisVertexWorldPos = go.transform.TransformPoint(vertices[v]);
float heightAtPoint = t.SampleHeight(thisVertexWorldPos);
vertices[v].y += heightAtPoint;
}
mesh.vertices = vertices;
}
Hi,
sorry that it took me so long to answer. Fortunately I started a C# course. At least I’m able to understand whats goin on.
So I tried your approach and sadly it doesn’t work:-(
I tried to debug it but this is all I found:
It seems it cant find or access (or whatever) the mesh. It stays null no matter what I do.
But it seems that this approach doesn’t work with ProBuilder objects.
With a simple unity cube at least the for loop is executed.
For now I’m out of ideas. I think I have to learn more about Probuilder or I have to ask there but this one seems too hard for me.
Or ist there any other way I should try?
I cant imagine it’s impossible to let the player build trenches ww1 style in runtime.
I already thought about creating trenches on top of the terrain but then the field of view wouldn’t be blocked from terrain elements like grass and whatever.
Well damn. But nonetheless I’m thankful for your help StarManta
It’s helpful to always copy and paste the exact error message. I can think of at least three possible exceptions it could be giving you based on the vague wording here, all of which have completely different solutions.
It’ll be helpful to find the MeshFilter component of these objects in the hierarchy and post a screenshot of the hierarchy+inspector windows. I don’t know how ProBuilder structures the objects it creates, maybe the MeshFilter is on a child object somewhere? A screenshot will help clear that up.
Also, your code uses .sharedMesh, where for this application you want to be using .mesh. .sharedMesh will alter the mesh for all GameObjects that share it; if you have ten copies of Trench_01 in the scene, their sharedMesh’s will all point to the same object. You’ll raise their vertices ten times, and most likely all 10 will be floating high above the landscape. Using .mesh will intelligently make a copy of the mesh and then return that copy, so you’ll only be altering the mesh of that one object.
Hi,
unfortunetaly there is no error message.
The debuginfos are the best I can get.
But well…while taking a sreenshot from the inspektor I took a closer look at “Pro Builder Mesh”. I clicked around and found “Edit script” and damn…It seems to have everything I need.
And in this code (ProBuilderMesh.cs)there is for example:
So I assume I have to make every instantiated trench (in this case “pb_Mesh17414”) to an object of the class probuildermesh and use its getters and setters?
No, you should be able to use GetComponent().mesh to access the mesh of the object.
Took another look at your code above, and I see the issue. You’re not altering the mesh of the object you instantiated: you’re attempting to alter the mesh of the prefab itself. It should look more like:
It’s possible that ProBuilder won’t immediately assign the mesh to the MeshFilter. I don’t see an Awake or Start function in ProBuilderMesh so who knows when it actually assigns the mesh. If the above doesn’t work, you may wish to add a one-frame delay with a coroutine or something before calling AlterHeightsOfMesh.
Ohhhh although pretty obvious I didn’t thought about that. And well now it seems like it’s working. I think this is what I wanted and now it should be possible to play around with terrainheight and aligning andandand .