0
\$\begingroup\$

https://youtu.be/uKblVWHaqn0

I don't understand why sometimes the sections are deactivating like they should... and sometimes they are not. All I'm trying to do is make sure the maze pieces can't be placed on top of each other (unless I want them too... users will get a power up where their tiles "crush" underlying tiles, so I have to be mindful of that feature now.

MazeManger.cs

  public static void SectionPlaced(GameObject mazeTile)
    {
        SectionValidation[] children;
        children = mazeTile.GetComponentsInChildren<SectionValidation>();
        foreach (SectionValidation sectionValidation in children)
        {
            
            if (instance.placedSections.Contains(sectionValidation.gameObject.transform.position))
            {
                Debug.LogError("placed two sections in same spot");
            }
            else
            {                
                instance.placedSections.Add(sectionValidation.gameObject.transform.position);
            }
        }
        EventManager.TriggerEvent("OnPlaceTile");
    }
}

BuildController.cs

  private void RotateGhost()
    {
        ghost.transform.Rotate(Vector3.up * 90.0f);
        currentRoty += 90.0f;
        if (currentRoty >= 360) currentRoty = 0.0f;
        
        SectionValidation[] children;
        children = ghost.GetComponentsInChildren<SectionValidation>(true);
        foreach (SectionValidation section in children)
        {
            section.gameObject.SetActive(true);
        }
        EventManager.TriggerEvent("OnMoveGhost");
    }

    private void PlaceMazeTile()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit hitInfo, 300f, mask))
        {
            if (!equippedTile) return;
            targetNode = hitInfo.transform.gameObject;

            GameObject placed = Instantiate(ghost, new Vector3(targetNode.transform.position.x, 0, targetNode.transform.position.z), Quaternion.Euler(0, currentRoty, 0));
            
            MazeManager.SectionPlaced(placed);

            if (ghost) Destroy(ghost);
        }
    }

    public void MoveGhost()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit hitInfo, 300f, mask))
        {
            if (!equippedTile) return;
            //only run the first time we hit a new node
            if (hitInfo.transform.gameObject == targetNode) return;
            targetNode = hitInfo.transform.gameObject;

            if (ghost) Destroy(ghost);
            ghost = Instantiate(equippedTile, new Vector3(hitInfo.transform.position.x, 0, hitInfo.transform.position.z), Quaternion.Euler(0, currentRoty, 0));
            EventManager.TriggerEvent("OnMoveGhost");
                     
        }

    }

SectionValidation.cs This is placed on every 1x1 square section of the tile... like minos making up a tetromino

 private void OnMoveGhost()
    {
        if (!isGhost) return;

        if(MazeManager.instance.placedSections.Contains(this.gameObject.transform.position))
        {
            this.gameObject.SetActive(false);
        }
    }
\$\endgroup\$
10
  • 2
    \$\begingroup\$ The video is a great addition to the question, but before you put the link, you might want to better described what it contains, what we need to look for (and when) and what you are expecting instead. \$\endgroup\$
    – Vaillancourt
    Jul 5 '21 at 14:02
  • \$\begingroup\$ First I do not advice storing, then checking transform.position directly. First convert them to discreet 'addresses' first to integers then to Vector2 (you aren't using 3 axes, are you?). Will 0.9999999... be equal to 1 ? It works for me - i also use tiles in the game \$\endgroup\$
    – ShoulO
    Jul 5 '21 at 14:12
  • \$\begingroup\$ Second I am still baffled why SectionValidation.cs is placed (only) on every 1x1 square section of the tile?? as I understood from video there are more sections then tiles? (I would recomend to validate only once before placing and not to assign validation to every tile) \$\endgroup\$
    – ShoulO
    Jul 5 '21 at 14:18
  • \$\begingroup\$ Third you could use hashtable (or dictionary) for your occupiedTiles, in such way you could store two things - tile adress as a key (Vector2) and type of building as value. Or the actual building gameObject as a value \$\endgroup\$
    – ShoulO
    Jul 5 '21 at 14:22
  • \$\begingroup\$ It is only checked as it is placed. The section validation is deactivated after they are placed. Doing further tests I've found this odd issue. Notice the position in the game world vs the stored transform vs manually typing in that same transform vs just typing in 0. youtube.com/watch?v=sQdlcpmrN_U&t=6s \$\endgroup\$
    – Astrydax
    Jul 5 '21 at 14:31
1
\$\begingroup\$

My advice would be to store only descreet adresses of tiles and not object positions directly. Convert them into integers before storing and checking.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .