#mazeSolving
1 messages · Page 1 of 1 (latest)
ok, so
from a general standpoint, what you want to do is manage two lists
a list of valid positions it can move to next
and a list of positions it has already visited
activeTiles - list you can move to next
visitedTiles - list you already visited
is your function called in update?
It's called via InvokeRepeating("MoveEmployee", 1.0f, 1.5f); in start
because I want there to be a delay every time the gameobject moves
ok
i think you want something like this
but look at it and see if it makes sense
{
visitedTiles.Add(currentPos);
activeTiles = new List<Vector2>();
Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);
if (!visitedTiles.Contains(up))
activeTiles.Add(up);
//Sets this employee to next position based on active tiles
Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
gameObject.transform.position = nextPosition;
//Add new position to visited tiles
visitedTiles.Add(nextPosition);
}
and you'd need to add the if...add for the other 3 directions
that makes sense yes, but for activeTiles don't i make that a class variable
you do if you want everything everywhere to be added, but if you want your little dude every turn to only pick one of the 4 nearby locations, then you don't
if you want them to be able to go anywhere at all that they haven't been, then get rid of the new list line for active tiles
private void MoveToRandomPos(Vector2 currentPos)
{
visitedTiles.Add(currentPos);
List<Vector2> activeTiles = new List<Vector2>();
Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);
activeTiles.Add(up);
activeTiles.Add(down);
activeTiles.Add(left);
activeTiles.Add(right);
//Add directions to active tiles if not visited
if (!visitedTiles.Contains(up))
activeTiles.Add(up);
if (!visitedTiles.Contains(down))
activeTiles.Add(down);
if (!visitedTiles.Contains(left))
activeTiles.Add(left);
if (!visitedTiles.Contains(right))
activeTiles.Add(right);
//Checks if new directions were visited, if so remove from active list
for (int i = activeTiles.Count; i < activeTiles.Count; i--)
{
if (visitedTiles.Contains(activeTiles[i]))
{
activeTiles.Remove(activeTiles[i]);
}
}
//Sets this employee to next position based on active tiles
Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
gameObject.transform.position = nextPosition;
//Add new position to visited tiles
visitedTiles.Add(nextPosition);
}```
Include the for loop?
I don't think you have to since we're checking visitedtiles already
no, get rid of for loop, and the add before the if's
{
visitedTiles.Add(currentPos);
List<Vector2> activeTiles = new List<Vector2>();
Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);
//Add directions to active tiles if not visited
if (!visitedTiles.Contains(up))
activeTiles.Add(up);
if (!visitedTiles.Contains(down))
activeTiles.Add(down);
if (!visitedTiles.Contains(left))
activeTiles.Add(left);
if (!visitedTiles.Contains(right))
activeTiles.Add(right);
//Sets this employee to next position based on active tiles
Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
gameObject.transform.position = nextPosition;
//Add new position to visited tiles
visitedTiles.Add(nextPosition);
}```
Right
does that make sense what its doing?
Yeah, we're declaring a new list every time the AI moved basically
in that list are the new directions
Okay great, that's part of the A* pathfinding done haha. Thank you @orchid mesa
and whats probably going to happen is your guy will wander around a bit and then corner himself and not move anymore
because he will have already visited all 4 of the nearby tiles
Right, when that happens I should clear visitedTiles
and wont have a valid place to go
if (up, down, left, right are visited) clear visited
ya, so you can add an if activetiles is empty at the end, clear visited tiles or something
now I need to set a direction and get the GO location
but I'll research some of that
I used this for reference
yep, what you're doing is really similar to a maze finding algorithm
which is why i called the thread that
yes
I did a maze solving algorithm for my data structures class last year
but it was last year
and in java
ya, concepts are similar, you just need to adapt to the language
not familiar with that
its like visual studio
yeah I already ceiltoint
It sucks because I have two locations for the ai
the global and the grid
global location to gridlocation formula is basically
object.x - (Horizontal - 0.5f), object.y - (Vertical - 0.5f)
and then you ceil it?