private void Update()
    {
        _moveInput = new Vector2(
            Input.GetAxis("Horizontal"),
            Input.GetAxis("Vertical"));

        _mousePosition = HelperFuncs.GetMousePositionWorld();
    }
Example #2
0
    private void Update()
    {
        if (!IsPlacing)
        {
            if (Input.GetButtonUp("TogglePlacing"))
            {
                StartPlacing(ObjectPrefab);
            }
            return;
        }

        // Update preview
        Vector2 position = HelperFuncs.GetMousePositionWorld();

        Vector2Int gridCoord = Grid.GetCoordFromCentre(position);

        position = Grid.GetCellCorner(gridCoord);

        bool isOccupied = GridObjMgr.IsOccupied(gridCoord, ObjectPrefab.OccupiedTiles);

        ObjectPreview.gameObject.SetActive(!isOccupied);
        ObjectPreview.transform.position = position;

        if (Input.GetButton("Place"))
        {
            if (!isOccupied)
            {
                Building building = Instantiate(ObjectPrefab, position, Quaternion.identity, GridObjMgr.transform);

                GridObjMgr.AddObject(building, gridCoord);
            }
        }

        if (Input.GetButtonUp("Cancel") ||
            Input.GetButtonUp("TogglePlacing"))
        {
            StopPlacing();
        }
    }