コード例 #1
0
    public void InactivateObject(GameObject obj)
    {
        if (_objectPool.Contains(obj))
        {
            if (obj != null)
            {
                StackableCube stackableCube = obj.GetComponent <StackableCube>();
                if (stackableCube != null)
                {
                    stackableCube.IsActive = false;
                }

                BoxCollider boxCollider = obj.GetComponent <BoxCollider>();
                if (boxCollider != null)
                {
                    boxCollider.isTrigger = false;
                }

                Rigidbody rb = obj.GetComponent <Rigidbody>();
                if (rb != null)
                {
                    rb.isKinematic = false;
                }

                obj.SetActive(false);
            }
            else
            {
                Debug.Log("Cannot inactivate object. Object is null");
            }
        }
    }
コード例 #2
0
    private void AddStackableCube(GameObject obj)
    {
        GameObject cube = _pool.GetObject();

        if (cube != null)
        {
            _stack.Add(cube);

            StackableCube controller = cube.GetComponent <StackableCube>();
            cubeSize            = controller.CubeSize;
            controller.IsActive = true;

            cube.gameObject.isStatic = false;
            cube.GetComponent <BoxCollider>().isTrigger = false;
            cube.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;

            Vector3 playerPosition  = _player.transform.position;
            Vector3 newCubePosition = new Vector3(playerPosition.x, playerPosition.y + pivotYOffset, playerPosition.z);
            cube.transform.position = newCubePosition;
            cube.transform.SetParent(transform);
            _player.transform.position = new Vector3(playerPosition.x, playerPosition.y + cubeSize, playerPosition.z);

            cube.SetActive(true);
        }
    }