Movement(validation) as a cross

Hey, I was wondering how to make the Killrange in a cross shape. But I can‘t figure it out myself, maybe somebody can help me?

Just don’t add the diagonals.

In GetValidActionGridPositionList() you can add the following

for (int x = -maxShootDistance; x <= maxShootDistance; x++)
{
    for (int z = -maxShootDistance; z <= maxShootDistance; z++)
    {
        // skip the diagonals
        if (x != 0 || z != 0) continue;

        // all the existing code here...
    }
}
1 Like

And if i only want to include diagonals, I just make it to:

if(x == 0 || z == 0) continue;

?

no, the diagonals are already included so you just remove the line again.

If you want only diagonals - to create an X - you could try

if (x != z) continue;

instead. I haven’t tested it but I think it should work. Makes sense in my head

2 Likes

Thank you for your time, that helped a lot :grinning:

So I thought it worked, but actually it made a line. Maybe I did something wrong?

for (int x = - maxMoveDistance; x <= maxMoveDistance; x++)
        {
            for (int z = -maxMoveDistance; z <= maxMoveDistance; z++)
            {
                GridPos offsetGridPos = new GridPos(x, z);
                GridPos testGridPos = unitGridPos + offsetGridPos;

                if (!LevelGrid.Instance.isValidGridPos(testGridPos))
                {
                    continue;
                }

                if (gameObject.GetComponent<Unit>().GetCurrentTeamId() == 0 && x != 0 || z != 0) //teamid is to check if its the right team
                {
                    continue;
                }

Yeah, it should be like this

for (int x = -maxShootDistance; x <= maxShootDistance; x++)
{
    for (int z = -maxShootDistance; z <= maxShootDistance; z++)
    {
        // skip the diagonals
        if (x != 0 || z != 0) continue;
        // or only diagonals
        //if (x != z) continue;

        GridPos offsetGridPos = new gridPos(x, z);
        GridPos testGridPos = unitGridPos + offsetGridPos;

        // etc., all the rest of the existing code here...
    }
}

weird, it doesn‘t work. Nothing is getting shown

OK sorry, that’s my bad. I checked it and should be this

// ignore diagonals
if (x != 0 && z != 0) continue;
// or, ONLY diagonals
//if (Mathf.Abs(x) != Mathf.Abs(z)) continue;

Also, there is another spot you’d have to change it. You also want to update GridSystemVisual.cs

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++)
        {
            // ignore diagonals
            if (x != 0 && z != 0) continue;
            // or, ONLY diagonals
            //if (Mathf.Abs(x) != Mathf.Abs(z)) continue;

            // ... the rest ...
        }
    }
}
// AND
private void ShowGridPositionRangeSquare(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++)
        {
            // ignore diagonals
            if (x != 0 && z != 0) continue;
            // or, ONLY diagonals
            //if (Mathf.Abs(x) != Mathf.Abs(z)) continue;

            // ... the rest ...
        }
    }
}
1 Like

Was the FOR-LOOP already there?

yes, you just insert the if

And why do I need to change it in GridSystemVisual, I mean what does the Code-Snippet do? That it needs to be fixed

The GridSystemVisual is what shows the valid positions. If you don’t change it, it will show positions that is now not valid because the actual shoot action cannot shoot there

Btw. I didn‘t finish the course completly so I don‘t see ShowGridPositionRangeSquare(). And I apologize for my bad understanding of code. + I needed this course for the Grid and logic, but im actually making my own game, that even is multiplayer. But i‘ll try to remember to add it if I ever completly finish it

I am referencing the full completed code base. If you don’t have that code yet, you can ignore it, but you will need to remember this for when you do come to that part of the course

Whatever im gonna try what you suggested

Oh, also maybe I have a important info, this cross-movement is only for 1 Team(for all the players in it)

Thank you so much! It worked, I think :grin:
Edit:
It did, to this point :+1:

I didn‘t need to change GridVisual, but maybe some errors will appear in the progress

And the other team only has the normal movement, perfect

Privacy & Terms