Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为创建的每个网格设置适当的碰撞器类型_C#_Unity3d_Mesh_Collider - Fatal编程技术网

C# 为创建的每个网格设置适当的碰撞器类型

C# 为创建的每个网格设置适当的碰撞器类型,c#,unity3d,mesh,collider,C#,Unity3d,Mesh,Collider,我正在制作一个基于3D分形的部件,在运行时生成一组基本的球体和胶囊网格对象,我一直在研究如何在创建每个对象时应用适当的碰撞器类型 using UnityEngine; using UnityEngine.UI; using System.Collections; public class BuildFractal : MonoBehaviour { public Mesh[] meshes; public Material material; public Mater

我正在制作一个基于3D分形的部件,在运行时生成一组基本的球体和胶囊网格对象,我一直在研究如何在创建每个对象时应用适当的碰撞器类型

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BuildFractal : MonoBehaviour 
{
    public Mesh[] meshes;
    public Material material;
    public Material[,] materials;
    private Rigidbody rb;

    public int maxDepth;             // max children depth
    private int depth;
    public float childScale;         // set scale of child objects
    public float spawnProbability;   // determine whether a branch is created or not
    public float maxRotationSpeed;   // set maximium rotation speed
    private float rotationSpeed;
    public float maxTwist;
    public Text positionText;
    public int objectMass;

    // Create arrays for direction and orientation data
    private static Vector3[] childDirections = {
        Vector3.up,
        Vector3.right,
        Vector3.left,
        Vector3.forward,
        Vector3.back,
        // Vector3.down
    };
    private static Quaternion[] childOrientations = {
        Quaternion.identity,
        Quaternion.Euler(0f, 0f, -90f),
        Quaternion.Euler(0f, 0f, 90f),
        Quaternion.Euler(90f, 0f, 0f),
        Quaternion.Euler(-90f, 0f, 0f),
        // Quaternion.Euler(180f, 0f, 0f)
    };

    private void Start () 
    {
        Rigidbody rb;

        rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
        transform.Rotate(Random.Range(-maxTwist, maxTwist), 0f, 0f);

        if (materials == null)
        {
            InitializeMaterials();
        }

        // Select from random range of meshes
        gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(0, meshes.Length)];
        // Select from random range of colors
        gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
        // Add Rigigbody to each object
        rb = gameObject.AddComponent<Rigidbody>();
        rb.useGravity = false;
        rb.mass = objectMass;

        // Create Fractal Children
        if (depth < maxDepth)
        {
            StartCoroutine(CreateChildren());
        }
    }

    private void Update ()
    {
        transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
    }

    private void Initialize (BuildFractal parent, int childIndex)
    {
        maxRotationSpeed = parent.maxRotationSpeed;

        // copy mesh and material references from parent object
        meshes = parent.meshes;
        materials = parent.materials;
        maxTwist = parent.maxTwist;

        // set depth and scale based on variables defined in parent
        maxDepth = parent.maxDepth;
        depth = parent.depth + 1;
        childScale = parent.childScale;

        transform.parent = parent.transform;           // set child transform to parent

        // transform.localScale = Vector3.one * childScale;

        transform.localScale = Vector3.one * Random.Range(childScale / 10, childScale * 1);
        transform.localPosition = childDirections[childIndex] * (Random.Range((0.1f + 0.1f * childScale),(0.9f + 0.9f * childScale)));
        transform.localRotation = childOrientations[childIndex];

        spawnProbability = parent.spawnProbability;
    }

    private void InitializeMaterials ()
    {
        materials = new Material[maxDepth + 1, 2];

        for (int i = 0; i <= maxDepth; i++)
        {
            float t = i / (maxDepth - 1f);
            t *= t;

            // Create a 2D array to hold color progressions
            materials[i, 0] = new Material(material);
            materials[i, 0].color = Color.Lerp(Color.gray, Color.white, t);
            materials[i, 1] = new Material(material);
            materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
        }
        // materials[maxDepth, 0].color = Color.white;
        materials[maxDepth, 1].color = Color.white;
    }

    private IEnumerator CreateChildren ()
    {
        for (int i = 0; i < childDirections.Length; i++)
        {
            if (Random.value < spawnProbability)
            {
                yield return new WaitForSeconds(Random.Range(0.1f, 1.5f));
                new GameObject("Fractal Child").AddComponent<BuildFractal>().Initialize(this, i);
            }
        }
    }

    /*void OnCollisionEnter(Collision col)
    {
        if(col.gameObject.name == "Fractal Child")
        {
            Destroy(this.gameObject);
        }
    }*/
}
我的脚本从球体或胶囊中随机选择,并根据一系列可能的方向排列它们。我需要有关如何在创建每个网格时指定适当碰撞器的帮助

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BuildFractal : MonoBehaviour 
{
    public Mesh[] meshes;
    public Material material;
    public Material[,] materials;
    private Rigidbody rb;

    public int maxDepth;             // max children depth
    private int depth;
    public float childScale;         // set scale of child objects
    public float spawnProbability;   // determine whether a branch is created or not
    public float maxRotationSpeed;   // set maximium rotation speed
    private float rotationSpeed;
    public float maxTwist;
    public Text positionText;
    public int objectMass;

    // Create arrays for direction and orientation data
    private static Vector3[] childDirections = {
        Vector3.up,
        Vector3.right,
        Vector3.left,
        Vector3.forward,
        Vector3.back,
        // Vector3.down
    };
    private static Quaternion[] childOrientations = {
        Quaternion.identity,
        Quaternion.Euler(0f, 0f, -90f),
        Quaternion.Euler(0f, 0f, 90f),
        Quaternion.Euler(90f, 0f, 0f),
        Quaternion.Euler(-90f, 0f, 0f),
        // Quaternion.Euler(180f, 0f, 0f)
    };

    private void Start () 
    {
        Rigidbody rb;

        rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
        transform.Rotate(Random.Range(-maxTwist, maxTwist), 0f, 0f);

        if (materials == null)
        {
            InitializeMaterials();
        }

        // Select from random range of meshes
        gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(0, meshes.Length)];
        // Select from random range of colors
        gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
        // Add Rigigbody to each object
        rb = gameObject.AddComponent<Rigidbody>();
        rb.useGravity = false;
        rb.mass = objectMass;

        // Create Fractal Children
        if (depth < maxDepth)
        {
            StartCoroutine(CreateChildren());
        }
    }

    private void Update ()
    {
        transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
    }

    private void Initialize (BuildFractal parent, int childIndex)
    {
        maxRotationSpeed = parent.maxRotationSpeed;

        // copy mesh and material references from parent object
        meshes = parent.meshes;
        materials = parent.materials;
        maxTwist = parent.maxTwist;

        // set depth and scale based on variables defined in parent
        maxDepth = parent.maxDepth;
        depth = parent.depth + 1;
        childScale = parent.childScale;

        transform.parent = parent.transform;           // set child transform to parent

        // transform.localScale = Vector3.one * childScale;

        transform.localScale = Vector3.one * Random.Range(childScale / 10, childScale * 1);
        transform.localPosition = childDirections[childIndex] * (Random.Range((0.1f + 0.1f * childScale),(0.9f + 0.9f * childScale)));
        transform.localRotation = childOrientations[childIndex];

        spawnProbability = parent.spawnProbability;
    }

    private void InitializeMaterials ()
    {
        materials = new Material[maxDepth + 1, 2];

        for (int i = 0; i <= maxDepth; i++)
        {
            float t = i / (maxDepth - 1f);
            t *= t;

            // Create a 2D array to hold color progressions
            materials[i, 0] = new Material(material);
            materials[i, 0].color = Color.Lerp(Color.gray, Color.white, t);
            materials[i, 1] = new Material(material);
            materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
        }
        // materials[maxDepth, 0].color = Color.white;
        materials[maxDepth, 1].color = Color.white;
    }

    private IEnumerator CreateChildren ()
    {
        for (int i = 0; i < childDirections.Length; i++)
        {
            if (Random.value < spawnProbability)
            {
                yield return new WaitForSeconds(Random.Range(0.1f, 1.5f));
                new GameObject("Fractal Child").AddComponent<BuildFractal>().Initialize(this, i);
            }
        }
    }

    /*void OnCollisionEnter(Collision col)
    {
        if(col.gameObject.name == "Fractal Child")
        {
            Destroy(this.gameObject);
        }
    }*/
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共类:单一行为
{
公共网格[]网格;
公共材料;
公共材料[,]材料;
私人刚体;
public int maxDepth;//最大子级深度
私有整数深度;
public float childScale;//设置子对象的比例
公共浮点概率;//确定是否创建了分支
公共浮点maxRotationSpeed;//设置最大转速
私人浮动旋转速度;
公共浮动maxTwist;
公共文本;
大众传媒;
//为方向和方向数据创建阵列
专用静态向量3[]子方向={
Vector3.up,
向量3.对,
向量3.左,
矢量3.前进,
向量3.back,
//矢量3.0向下
};
私有静态四元数[]子方向={
四元数,
四元数欧拉(0f,0f,-90f),
四元数欧拉(0f,0f,90f),
四元数欧拉(90f,0f,0f),
四元数欧拉(-90f,0f,0f),
//四元数欧拉(180f,0f,0f)
};
私有void开始()
{
刚体rb;
旋转速度=随机范围(-maxRotationSpeed,maxRotationSpeed);
变换.旋转(随机范围(-maxTwist,maxTwist),0f,0f);
if(materials==null)
{
初始化材料();
}
//从网格的随机范围中选择
gameObject.AddComponent().mesh=mesh[Random.Range(0,mesh.Length)];
//从随机的颜色范围中选择
gameObject.AddComponent().material=材质[深度,随机。范围(0,2)];
//将Rigigbody添加到每个对象
rb=gameObject.AddComponent();
rb.useGravity=false;
rb.mass=物体质量;
//创建分形子对象
if(深度<最大深度)
{
start例程(CreateChildren());
}
}
私有无效更新()
{
transform.Rotate(0f,rotationSpeed*Time.deltaTime,0f);
}
私有void初始化(BuildFractal父级,int childIndex)
{
maxRotationSpeed=parent.maxRotationSpeed;
//从父对象复制网格和材质参照
meshes=parent.meshes;
materials=父级。物料;
maxTwist=parent.maxTwist;
//根据父级中定义的变量设置深度和比例
maxDepth=parent.maxDepth;
深度=parent.depth+1;
childScale=parent.childScale;
transform.parent=parent.transform;//将子转换设置为parent
//transform.localScale=Vector3.one*childScale;
transform.localScale=Vector3.one*Random.Range(childScale/10,childScale*1);
transform.localPosition=childDirections[childIndex]*(Random.Range((0.1f+0.1f*childScale),(0.9f+0.9f*childScale));
transform.localRotation=childorients[childIndex];
繁殖概率=父代。繁殖概率;
}
私有void初始化材质()
{
材料=新材料[maxDepth+1,2];

对于(int i=0;i在实例化并设置相同网格后添加网格碰撞器,工作完成


这应该以编程的方式解决问题

你能将碰撞器应用于网格的特定部分,并使其成为预制件,并从预制件范围而不是网格范围中进行选择吗?这是个好主意,但作为项目要求的一部分,我试图以编程的方式完成所有工作。有没有想过如何在没有预先设置的情况下实现这一点晶圆厂?使用多边形碰撞器怎么样?它们通常会自动检测形状并尽可能地拟合它。因为您只使用
网格
数组,不幸的是,没有任何方法可以回溯或轻松确定最相似的原始形状。我认为预设选项是可行的,否则我会看到大量的数学和数百种方法代码行减慢了一切,只是为了做事先可以准备的事情。