Grid Visuals are showing up in mid air

It is visable in the tutorial video too. Unless it is something that is handled in a future video there are two things missing.

In the GridSystemVisual class The ShowGridPositionRange is still using 0 for the floor value instead of gridPosition.floor.

 GridPosition testGridPosition = gridPosition + new GridPosition(x, z, gridPosition.floor);

Also in both the ShootAction and GrenadeAction we need to add an extra check in GetValidActionGridPositionList to check that the GridPosition is Walkable so that we only highlight targets on the ground::

if (!Pathfinding.Instance.IsWalkableGridPosition(testGridPosition))
                    {
                        continue;
                    }

It shouldn’t be. The GridPosition passed into the ShowGridPositionRange() method should already have the floor baked into it. What we’re adding is just the x and y offsets as we’re considering tiles on the same floor.

    private void ShowGridPositionRange(GridPosition gridPosition, int range, GridVisualType gridVisualType)
    {
        List<GridPosition> gridPositionList = new List<GridPosition>();

        for (int x = -range; x <= range; x++)
        {
            for (int z = -range; z <= range; z++)
            {
                GridPosition testGridPosition = gridPosition + new GridPosition(x, z, 0);

                if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
                {
                    continue;
                }

                int testDistance = Mathf.Abs(x) + Mathf.Abs(z);
                if (testDistance > range)
                {
                    continue;
                }

                gridPositionList.Add(testGridPosition);
            }
        }

        ShowGridPositionList(gridPositionList, gridVisualType);
    }

So if our gridPosition passed into the method is GridPosition(10,10,2), then the result of

GridPosition testGridPosition = gridPosition + new GridPosition(x, z, 0);

should have the floor set to the same value as gridPosition.floor.
In fact, adding gridPosition.floor to the equation should actually give results past the 1st floor (0) that are floor*2 higher…

Yes you are correct by adding grid position.floor I was simply making it so that the position was too high and thus invalid. Which does partially make the issue less obvious go away but, not really a valid fix. For the ShowGridPositionRange we should not be checking the current Floor. We should be checking all floors. Like we do in GetValidActionGridPositionList and also further checking that the squares are walkable.

I’ve watched the series till the end and this bug is still visible in the last video.

Screenshot 2023-10-07 at 10.34.38

Notice the red squares on multiple levels. Also the correct targetable enemy unit is highlighted showing that there is a difference in the squares being highlighted by the ShowGridPositionRange in GridVisual and the actual squares used by GetValidActionGridPositionList in the ShootAction.

I can see the red square on the character, but am seeing other red squares… that being said, I may not be the right person to ask, as I have a red/green color deficiency (I tend to mix other colors into my reds when I use red as a marker, or make something stronger like a glow effect that even my eyes can’t miss).

Ok, jumped into the project again and changed the color of the redsoft material to blue…


I think I see what’s going on. We’re testing to ensure that each position is a valid grid position, but for the floor, that really means we’re just testing that the floor exists and that it’s between 0 and the grid size… we’ll need to take an additional step and check the Pathfinding to ensure that it’s walkable.

GridPosition testGridPosition = gridPosition + new GridPosition(x, z, 0);

                if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
                {
                    continue;
                }
                //Added this to method
                if (!Pathfinding.Instance.IsWalkableGridPosition(testGridPosition)) continue;

                int testDistance = Mathf.Abs(x) + Mathf.Abs(z);
                if (testDistance > range)
                {
                    continue;
                }

                gridPositionList.Add(testGridPosition);

1 Like

Yes this is exactly it and is the solution I have gone with. Although it is only a partial fix as it does mean that it no longer shows your range (if there are no targets in your vicinity it looks like the shoot action does nothing) and also means you cannot shoot crates or land a grenade on top of them (if you wanted your game to do this).

I’m currently moving all of this over to Godot so I’ll look at a more comprehensive solution when I get to reimplementing this part.

That would require a touch of extra engineering in the Pathfinding class (since it is the class marking the positions as walkable or not walkable).

The path finding node would need, in addition to isWalkable, an isDestructible, which would be set by testing the result of the raycast when it sets up to see if the hit is a destructible and setting it accordingly.

Then a method simlar to IsWalkableGridPosition() would test if the position is walkable or if it is targetable…
Something like

    public bool IsTargetableGridPosition(GridPosition gridPosition)
    {
        if (IsWalkableGridPosition(gridPosition)) return true;
        return GetGridSystem(gridPosition.floor).GetGridObject(gridPosition).IsTargetable();
    }

Privacy & Terms