Example #1
0
File: GUI.cs Project: Hengle/LD43
    void SpawnObject(Vector3 position, Vector3 normal)
    {
        if (rootContainer != null)
        {
            UndoTransformInRoot();
        }
        // Get selected gameobject
        List <SpawnableObject> objs = spawnableObjects.FindAll(
            delegate(SpawnableObject s)
        {
            return(s.spawn == true);
        });

        if (objs.Count == 0)
        {
            return;
        }
        int rndObj = UnityEngine.Random.Range(0, objs.Count);

        SpawnableObject selectedObject = objs[rndObj];
        Vector3         positionUp     = position;

        if (objGlobalSettings.spawn)
        {
            positionUp.y = positionUp.y + objGlobalSettings.offset;
        }
        else
        {
            positionUp.y = positionUp.y + selectedObject.offset;
        }
        GameObject newObj = Instantiate(selectedObject.prefab, positionUp, Quaternion.identity);

        // Apply random factors
        if (objGlobalSettings.spawn)
        {
            SpawnableObject.ApplyRandomFactors(objGlobalSettings, newObj.transform);
        }
        else
        {
            SpawnableObject.ApplyRandomFactors(selectedObject, newObj.transform);
        }

        // We add a box collider if no box collider is present.
        if (newObj.GetComponent <Collider>() == null && PhysicsUtilities.physicsScatter)
        {
            newObj.AddComponent <BoxCollider>();
        }

        // If a collider is present and is a meshCollider, we have the problem that mesh colliders are not supported anymore. However, sometime when convex is set to true and error is thrown and convex fails. If we inflate the Mesh first,
        // the convex mesh is created correctly.
        if (newObj.GetComponent <MeshCollider>() != null && PhysicsUtilities.physicsScatter && newObj.GetComponent <MeshCollider>().convex == false)
        {
            newObj.GetComponent <MeshCollider>().inflateMesh = true;
            newObj.GetComponent <MeshCollider>().convex      = true;
        }

        // We add a rigidbody if not present
        if (newObj.GetComponent <Rigidbody>() == null && PhysicsUtilities.physicsScatter)
        {
            newObj.AddComponent <Rigidbody>();
        }
        // If there is a rigidbody, and is kinematic, we set kinematic to false.
        if (newObj.GetComponent <Rigidbody>() != null && PhysicsUtilities.physicsScatter && newObj.GetComponent <Rigidbody>().isKinematic == true)
        {
            newObj.GetComponent <Rigidbody>().isKinematic = false;
        }

        if (newObj.GetComponent <Rigidbody>() != null && PhysicsUtilities.physicsScatter == false)
        {
            DestroyImmediate(newObj.GetComponent <Rigidbody>());
        }


        newObj.transform.parent = returnOrCreateRootContainer().transform;
        Undo.RegisterCreatedObjectUndo(newObj, "Physics Scatter : new objectS");

        PhysicsUtilities.ActivatePhysics();
    }