示例#1
0
    private void PlaceCubeNear(Vector3 clickPoint)
    {
        var finalPosition = grid.GetNearestPointOnGrid(clickPoint);

        GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;

        //GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = nearPoint;
    }
示例#2
0
    void Update()
    {
        TextCosts.text = Costs.ToString();

        this.GetComponent <Button> ().interactable = gameControl.Gold >= Costs;

        if (creatingActive)
        {
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                bool       done     = false;
                Vector3    mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y + BuildOffsetY, 0f);
                Ray        ray      = Camera.main.ScreenPointToRay(mousePos);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 9999, LayerMask.NameToLayer("test")))
                {
                    preObject.transform.position = gridScript.GetNearestPointOnGrid(hit.point);
                    preObject.SetActive(true);
                }
                else
                {
                    preObject.SetActive(false);
                }

                // Left mouse-klick, Build!
                if (Input.GetKeyUp(KeyCode.Mouse0) && preObject.GetComponent <TowerMechanic> ().CreatingAllowed)
                {
                    counter++;
                    createdObject       = Instantiate(ObjectToBeCreated, preObject.transform.position, preObject.transform.rotation);
                    createdObject.name += counter.ToString();
                    createdObject       = null;
                    done              = true;
                    gameControl.Gold -= Costs;
                }

                // Right mouse-klick, Abbort!
                if (Input.GetKey(KeyCode.Mouse1) | done)
                {
                    if (createdObject != null)
                    {
                        Destroy(createdObject);
                    }
                    if (preObject != null)
                    {
                        Destroy(preObject);
                    }
                    creatingActive = false;
                }
            }
        }
    }