using UnityEngine;
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class CustomPlane : MonoBehaviour {
public float width = 1;
public float length = 1;
private float oldWidth;
private float oldHeight;
public void Start()
{
oldWidth = width;
oldHeight = length;
Create();
}
private void Update()
{
if(oldWidth != width || oldHeight != length)
{
Create();
oldWidth = width;
oldHeight = length;
}
}
private void Create()
{
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[4]
{
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
new Vector3(0, length, 0),
new Vector3(width, length, 0)
};
mesh.vertices = vertices;
int[] tris = new int[6]
{
// lower left triangle
0, 2, 1,
// upper right triangle
2, 3, 1
};
mesh.triangles = tris;
Vector3[] normals = new Vector3[4]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
mesh.normals = normals;
Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
mesh.uv = uv;
meshFilter.mesh = mesh;
}
}
TheLater, the problem is later I want to fire a ray on the plane adand that ray will also get to a terrain. The plane is above the terrain in the air I want to shoot ray on the plane and that it will spawn objects too also on the terrain.
The plane is above the terrain in the air I want to shoot a ray on the plane and that it will spawn objects too also on the terrain.
This spawn objects on the plane but not on the terrain I think because the plane collider blockblocks it to get to the terrain.
Then I added in the editor a new layer at index 8 name Terrain and changed the Terrain layer to Terrain and in this variable terrainLayer I set it to Terrain too but it didn't work it's spawning the objects on the same position on the terrain and not on the plane at all.
