2

我正在运行时在我正在处理的基于 3D 分形的部分中生成一堆基本的球体和胶囊网格对象,并且我坚持在创建每个对象时如何应用适当的 Collider 类型。

我的脚本从球体或胶囊中随机选择,并根据一系列可能的方向排列它们。我需要有关如何在创建每个网格时分配适当的 Collider 的帮助。

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);
        }
    }*/
}
4

1 回答 1

1

实例化并设置相同的网格后添加网格碰撞器,完成!

http://docs.unity3d.com/ScriptReference/MeshCollider.html

那应该以编程方式对其进行排序

于 2015-11-04T23:34:47.713 回答