- Home /
How Can I make 3D Roads?
So the title kind of explains it, I need to make 3D roads similar to Unturned's road system how do I accomplish this?
I should probably note that it will need to go across the terrain too, so it has to be able to manipulate itself to the terrain and look natural, I have a 3D model for the road but I just need to be able to place it via nodes and the need to connect to each other.
Thanks in advance!
Also sorry for asking a lot of questions lately, I don't know if that bothers some people or not but I just recently found out that I was supposed to leave my questions in "Default" and they get answered now so I can finally get some assistance on all of the problems I've been having, most have been resolved thanks to you guys!
Answer by cjdev · May 28, 2016 at 07:05 AM
What you want to do isn't trivial but it looks like that road system you linked creates a procedural mesh by connecting quads between given waypoints. If that's what you're after then you'll need to start by creating a single quad procedurally and then expanding by increasing the number of vertices for each road section. Here's the bare-bones of how to create a grid of quads procedurally:
public void CreateMesh(int size)
{
List<Vector3> verts = new List<Vector3>(); // Index used in tri list
List<int> tris = new List<int>(); // Every 3 ints represents a triangle
List<Vector2> uvs = new List<Vector2>(); // Vertex in 0-1 UV space
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
verts.Add(new Vector3(i, 0, j));
uvs.Add(new Vector2((float)i / size, (float)j / size));
if (i == 0 || j == 0) continue; // First bottom and left skipped
tris.Add(size * i + j); //Top right
tris.Add(size * i + (j - 1)); //Bottom right
tris.Add(size * (i - 1) + (j - 1)); //Bottom left - First triangle
tris.Add(size * (i - 1) + (j - 1)); //Bottom left
tris.Add(size * (i - 1) + j); //Top left
tris.Add(size * i + j); //Top right - Second triangle
}
}
Mesh mesh = new Mesh();
mesh.vertices = verts.ToArray();
mesh.uv = uvs.ToArray();
mesh.triangles = tris.ToArray();
mesh.RecalculateNormals();
GameObject grid = new GameObject("Grid");
grid.AddComponent<MeshFilter>();
grid.AddComponent<MeshRenderer>();
grid.GetComponent<MeshFilter>().mesh = mesh;
// Load a material named "GridMat" from a folder named "Resources"
MaterialgridMat = Resources.Load<Material>("GridMat");
grid.GetComponent<Renderer>().material = gridMat;
}
That should be enough to get you started, but you'll have to adjust it to be a "grid" with one base square and x waypoints for height along with the custom positions from the waypoints. As for connecting the roads... that's a whole other beast entirely and one you might want to focus on once you have roads in place.
Thank you very much! Sadly it's 3:20 A$$anonymous$$ where I am and I don't have time to test it, I will try it first thing in the morning though.
Thank you for taking your time to make this! :)
Also, quick question; is this an editor script or how does it work?
It's a method that you would put in a script on a GameObject in your scene and call when you wanted to make a grid of size x size.
I'm not looking to make roads at runtime, I just want to make them baked into the map so that there are roads in the game.
I cannot model each road themselves because I don't think it's possible to open your Unity terrain in Blender, is it?
That is significantly easier, and yes you can export your terrain as an obj file and use it as a template to model your roads in Blender. Take a look at this script, should get you started.
Thank you very very much!
I have not tested it yet but it's also JavaScript which is good for me.
I'm going to sleep in about 20 $$anonymous$$utes, after I watch an episode of a show, but does this export textures as well? If so I can literally stop using the default Unity terrain and just use my prefab trees and finally make caves and such.
Thank you again for the help!
I'm really starting to love Unity Answers. xD
Your answer
Follow this Question
Related Questions
using height brush on plane? 2 Answers
How would one achieve the cliffs in dota2 terrain? 2 Answers
Painting Roads on Terrain 1 Answer
how to produce endless terrain illusion? 1 Answer
How to manipulate the Shape of a 3D object at runtime 1 Answer