UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a baseplate"
using UnityEngine; public class BasePlate : MonoBehaviour { public Vector3 basePlateSize = new Vector3(10, 1, 10); public Material basePlateMaterial; // Start is called before the first frame update void Start() { CreateBasePlate(); } void CreateBasePlate() { // Create a new GameObject for the baseplate GameObject basePlate = new GameObject("BasePlate"); // Add a MeshFilter and MeshRenderer component MeshFilter meshFilter = basePlate.AddComponent<MeshFilter>(); MeshRenderer meshRenderer = basePlate.AddComponent<MeshRenderer>(); // Create a new plane mesh and assign it to the MeshFilter Mesh basePlateMesh = new Mesh(); basePlateMesh = CreatePlane(basePlateSize.x, basePlateSize.z); meshFilter.mesh = basePlateMesh; // Assign the material to the MeshRenderer meshRenderer.material = basePlateMaterial; // Set the position and scale of the baseplate basePlate.transform.position = new Vector3(0, -basePlateSize.y / 2, 0); basePlate.transform.localScale = new Vector3(basePlateSize.x, basePlateSize.y, basePlateSize.z); } Mesh CreatePlane(float width, float height) { Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[4]; int[] triangles = new int[6]; // Define the vertices vertices[0] = new Vector3(-width / 2, 0, -height / 2); vertices[1] = new Vector3(-width / 2, 0, height / 2); vertices[2] = new Vector3(width / 2, 0, height / 2); vertices[3] = new Vector3(width / 2, 0, -height / 2); // Define the triangles triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; triangles[3] = 0; triangles[4] = 2; triangles[5] = 3; // Assign the vertices and triangles to the mesh mesh.vertices = vertices; mesh.triangles = triangles; // Recalculate the mesh normals mesh.RecalculateNormals(); return mesh; } }