コード例 #1
0
    private void Update()
    {
        if ((Input.GetMouseButtonDown(0) || Input.touchCount > 0) && TransparentCube != null && !EventSystem.current.IsPointerOverGameObject() && !IsLoose)
        {
#if !UNITY_EDITOR // If Application isn't started at Unity Editor, then
            if (Input.GetTouch(0).phase != TouchPhase.Began)
            {
                return;                                              // if it isn't a simple touch, then leave function
            }
#endif
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;                                                // if we pressed on GUI button, then we won't start game process
            }
            if (!FirstCube)
            {
                FirstCube = true;
                foreach (GameObject obj in CanvasStartPage)
                {
                    Destroy(obj);
                }
            }


            TowerCube.GetComponent <MeshRenderer>().material = AllMaterialsForCubes[UnityEngine.Random.Range(0, AvailableSize)];
            GameObject newCube = Instantiate(
                TowerCube,                // What object need to create
                TransparentCube.position, // Place for created object
                Quaternion.identity) as GameObject;

            // Sound Effect
            PlaySoundEffect();

            GameObject SpawnCubeEffectObject = Instantiate(SpawnCubeEffect,        // Create Spawn Effect
                                                           TransparentCube.position,
                                                           Quaternion.identity) as GameObject;
            Destroy(SpawnCubeEffectObject, 1f);               // Delete Spawn effect for optipization

            newCube.transform.SetParent(TowerHeap.transform); // Input new object in TowerHeap
            lastCube.setPosition(TransparentCube.position);   // Set the position of last cube
            SpawnedPositions.Add(lastCube.getPosition());     // Mark position as spawned

            //Camera Change
            ChangeCameraPosition_Background();

            //

            TowerHeapRB.isKinematic = true;
            TowerHeapRB.isKinematic = false;

            SpawnPositions();
        }

        CameraPosition.localPosition = Vector3.MoveTowards(CameraPosition.localPosition,
                                                           new Vector3(CameraPosition.localPosition.x, lastCube.y + MoveToY, CameraPosition.localPosition.z),
                                                           MoveCameraSpeed * Time.deltaTime); // Делает передвижение камеры плавны

        if (TowerHeapRB != null && TowerHeapRB.velocity.magnitude > 0.2f && !IsLoose)
        {
            Destroy(TransparentCube.gameObject);
            IsLoose = true;
            StopCoroutine(showCubePlace);
            Debug.Log("Game Over!");
        }

        if (Camera.main.backgroundColor != CurrentBackgroundColor)
        {
            Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, CurrentBackgroundColor, Time.deltaTime);
        }
    }