Example #1
0
    void Update()
    {
        if (HasBuilt)
        {
            return;
        }

        if (!CanBuild)
        {
            _renderer.material = _materials.ErrorMat;
        }
        else
        {
            _renderer.material = _materials.TransparentMat;
        }

        Ray        mouseray = _camera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(mouseray.origin, mouseray.direction, out hit, 100))
        {
            //snap to grid based on cell size
            transform.position = new Vector3(Mathf.RoundToInt(hit.point.x / _cellSize.x) * _cellSize.x, transform.position.y, Mathf.RoundToInt(hit.point.z / _cellSize.y) * _cellSize.y);
        }

        if (Input.GetMouseButtonUp(0) && !ClickAndDragController.instance.IsDragging)
        {
            if (CanBuild)
            {
                _renderer.material = _materials.NormalMat;
                _controller.BuildMe(transform.position);
            }
        }
    }