- Home /
Problem is not reproducible or outdated
Randomly Spawning 4 cherries in Pacman game (Unity)
At the moment i am spawning 4 cherries in my A* Pathfinding Map but they are being randomly spawning even under the boxes. I want my cherries to spawn in the Blue area of the A*Star path as shown in the image attached. Currently i have this code which is spawning the cherries attached to the Pathfinding grid.
Answer by ray2yar · Jan 19, 2019 at 01:40 PM
It appears your GeneratedPosition() routine just returns a randomly selected vector3. You need to check the position it selects against your nodes to see if the position is walkable. Here is some pseudo code (best I can do since I don't see you node code in your sample)
Vector3 GeneratedPosition () {
int x;
int y;
x = Random.Range(-14, 14);
y = Random.Range(-14, 14);
//Pseudo code for A*
if (Node[x, y] == "walkable") return new Vector3(x,0, y);
//This code should work if you use unitys built in navmesh
//assuming your 3d map is oriented so the 3d y value is 0 and your
//value is the zed
Vector3 Sample = new Vector3(x, 0, y);
//check the sample vector against a navmesh
NavMeshHit Hit;
if (NavMesh.SamplePosition(Sample, out Hit, 2f, NavMesh.AllAreas))
{
return Hit.position;
} else
{
return Vector3.zero;
}
}
I can show you the AStar Path code so you can better understand. Cause im not using the built in navmesh. Thanks
Follow this Question
Related Questions
Set AI Destination Setter target from Script 2 Answers
Multiple Cars not working 1 Answer
Side scroller spawns at different Y 1 Answer
Distribute terrain in zones 3 Answers