Hi! I’m new to Unity and my project is to make a prefab out of primitives. So, I decided to try to make a house, I was able to do it mostly with cubes of varying sizes, but when it comes to the roof, if I want the triangular shaped kind I need a triangle for the front and back of the roof. I read that you need a modeling program to make it. I am not a fan of that situation since I have never used one, is there really no way to create a triangle in Unity without modeling software? It just seems so strange they give you Cubes and Spheres and even Capsules but NOT Triangles, doesn’t? Even more strange is that the Asset Store seems to be devoid of anything triangles when I tried looking for one.
There is no builtin triangle primitive. Luckily the Unity Editor is easy to extend. Put this script somewhere in your project folder and a new “Triangle” button should pop up in the GameObject/3D Object menu.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
#endif
public class TrianglePrimitive {
#if UNITY_EDITOR
private static Mesh CreateMesh() {
Vector3[] vertices = {
new Vector3(-0.5f, -0.5f, 0),
new Vector3(0.5f, -0.5f, 0),
new Vector3(0f, 0.5f, 0)
};
Vector2[] uv = {
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0.5f, 1)
};
int[] triangles = { 0, 1, 2 };
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();
return mesh;
}
private static GameObject CreateObject() {
var obj = new GameObject("Triangle");
var mesh = CreateMesh();
var filter = obj.AddComponent<MeshFilter>();
var renderer = obj.AddComponent<MeshRenderer>();
var collider = obj.AddComponent<MeshCollider>();
filter.sharedMesh = mesh;
collider.sharedMesh = mesh;
renderer.sharedMaterial = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Material.mat");
return obj;
}
[MenuItem("GameObject/3D Object/Triangle", false, 0)]
public static void Create() {
CreateObject();
}
#endif
}
Obliged, this was handy.
I start by saying that you should learn Blender, it is free and easy, and it’s a miracle if you learn to use it.
You can also use sketchfab and get free models, there is a model of a triangle, just drag it into unity and your are good to go:
Free triangle model