A Pathfinding Agent - Project Report

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

A* PATHFINDING AGENT-

PROJECT REPORT

Graphic Era Hill University


Mini Project (2022-2023)

Made by: Aman Kumar

1. Introduction:
The purpose of this project was to develop a path finding agent using the A* (A-star) algorithm,
implemented with the Unity Nav Mesh Agent component. The project aimed to create an
intelligent agent capable of navigating through complex environments and finding the optimal
path from a starting point to a goal point. This report provides an overview of the project,
including its goals, methodology, implementation details, and results.

2. Project Goals:
The primary goals of the project were as follows:

Develop a path finding system using the A* algorithm.

Utilize the Unity Nav Mesh Agent to enable realistic navigation within the game
environment.

Optimize the path finding algorithm for efficiency and performance.

Implement a user-friendly interface for specifying the start and goal points.

Evaluate the performance and effectiveness of the path finding agent in different scenarios.

A* PATHFINDING AGENT- PROJECT REPORT 1


3. Methodology:
The project followed the following methodology:

Designing the navigation environment: A virtual game environment was created using
the Unity engine, consisting of obstacles, terrain, and the path finding agent.

Implementing the A* algorithm: The A* algorithm was implemented to calculate the


optimal path from the start to the goal point. The algorithm considered the cost of each path
and estimated the remaining cost using heuristics.

Integrating the Unity Nav Mesh Agent: The Unity Nav Mesh Agent component was
utilized to enable smooth and realistic navigation for the path finding agent.

User interface development: A user-friendly interface was designed to allow users to


specify the start and goal points within the game environment.

Testing and evaluation: The path finding agent was tested and evaluated in various
scenarios to assess its performance, efficiency, and accuracy.

4. Levels
4.1 Level 1: Basic Pathfinding
The first level showcased the agent's ability
to navigate through a simple maze. The
maze contained walls and corridors, and the
agent had to find the shortest path from its
starting position to the goal. The A* algorithm
efficiently computed the optimal path, and the
Nav Mesh Agent guided the agent through
the maze, avoiding obstacles and walls.

4.2 Level 2: Off-Mesh Links and Climbing


In the second level, the agent faced more
complex challenges. It had to navigate
through areas where off-mesh links were
present. These links represented non-
walkable areas or obstacles that required the

A* PATHFINDING AGENT- PROJECT REPORT 2


agent to jump or climb. The Nav Mesh Agent
successfully handled these situations,
allowing the agent to jump over gaps and
climb stairs and slopes to reach its
destination.

4.3 Level 3: Dynamic Obstacles


The third level introduced dynamic obstacles
that moved within the environment. These
obstacles posed a real-time challenge for the
agent as it had to adapt and find alternative
paths to reach the goal. The A* algorithm
continuously recalculated the optimal path,
taking into account the movement of the
obstacles. The Nav Mesh Agent efficiently
guided the agent, avoiding collisions with the
moving objects and finding the best route to
the goal.

A* PATHFINDING AGENT- PROJECT REPORT 3


5. Implementation Details:
The project implementation involved the following key components and techniques:

5.1 Unity Nav Mesh Agent:


The Unity Nav Mesh Agent was a crucial component in the implementation of the path
finding agent. It provided built-in functionality for realistic navigation within the game
environment. The Nav Mesh Agent utilized a navigation mesh, also known as a NavMesh,
which was a simplified representation of the game world. The NavMesh was generated by
baking the scene, analyzing the walkable surfaces, and creating a navigation graph.

void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))


{
agent.destination = hit.point;
}
}
}

The Nav Mesh Agent was responsible for the movement of the path finding agent. It took
care of path calculation, obstacle avoidance, and smooth movement. By utilizing the Nav
Mesh Agent, the agent could navigate around dynamic obstacles, such as other moving
entities, while still maintaining the overall path finding goal.

A* PATHFINDING AGENT- PROJECT REPORT 4


A* PATHFINDING AGENT- PROJECT REPORT 5
5.2 A Algorithm:*
The A* algorithm served as the core path finding algorithm in this project. It was a widely-
used and effective algorithm for finding the optimal path from a starting point to a goal point
on a graph. The algorithm explored the graph by considering both the cost of the current
path and an estimate of the remaining cost to the goal, known as the heuristic.
In the implementation, the A* algorithm utilized a graph representation of the game
environment. Each valid position in the environment was represented as a node, and
connections between nodes were represented as edges. The algorithm used a priority
queue, such as a min-heap, to efficiently explore the graph and expand nodes based on
their total cost, which was the sum of the path cost and the heuristic estimate.

5.3 Heuristics:
Heuristics played a crucial role in the A* algorithm by providing an estimate of the remaining
cost from a given node to the goal node. Different heuristics could be used, such as

A* PATHFINDING AGENT- PROJECT REPORT 6


Euclidean distance or Manhattan distance, depending on the nature of the game
environment and the desired behavior of the path finding agent.
The choice of heuristic impacted the efficiency and accuracy of the path finding process. A
good heuristic should be admissible, meaning it never overestimates the remaining cost, and
consistent (or monotonic), meaning the heuristic estimate is always less than or equal to the
cost of reaching the neighboring node plus the heuristic estimate of the neighboring node.
These properties ensured that the A* algorithm would find the optimal path.

5.4 Graph Representation:


The game environment was represented as a graph, with nodes representing valid positions
and edges representing connections between nodes. The graph was generated by analyzing
the walkable surfaces in the scene and determining the navigable areas. This graph
representation allowed for efficient path finding by reducing the search space to only valid
positions.

The graph representation also facilitated the integration of the Unity Nav Mesh Agent
component. The NavMesh baking process generated the navigation mesh, which was
essentially a pre-computed graph that the Nav Mesh Agent used for navigation. The Nav
Mesh Agent could then navigate through the nodes and edges of the NavMesh, avoiding
obstacles and finding the optimal path.

5.5 Unity UI:


The Unity UI system was utilized to create a user-friendly interface for the project, consisting
of a main menu and level selection screen. The UI design focused on providing easy
navigation and control over the game environment, including the ability to add more AI
agents to the scene.

A* PATHFINDING AGENT- PROJECT REPORT 7


Main Menu:
The main menu served as the home page of the game and featured a clean and intuitive
layout. It included buttons for three different levels, allowing the user to select the desired
level to play. Additionally, an exit button was provided to close the game.
The main menu provided a straightforward way for users to access the different levels
available, making it easy to navigate through the game's content.
Level Selection Screen:
Each level in the game had its own dedicated level selection screen. Upon selecting a
specific level from the main menu, the user was directed to the corresponding level
selection screen. This screen provided additional options and functionality specific to the
selected level.

public void OnButtonClick()


{
SceneManager.LoadScene(levelIndex);
}

On the level selection screen, two buttons were included. The first button was the "Back"
button, which allowed the user to return to the main menu and select a different level.
This feature ensured flexibility and allowed users to explore various levels without having
to close and reopen the game.

The second button on the level selection screen was the "+" button, which allowed the
user to add more AI agents to the scene. By clicking this button, the user could
dynamically increase the number of AI agents present in the level, enhancing the
complexity and challenge of the gameplay. This feature added an element of
customization and provided the user with control over the number of AI agents they
wanted to interact with.
The UI elements on the level selection screen were designed to be visually appealing
and easy to understand, contributing to a seamless and immersive user experience.

5.6 Additional Implementation Details:


In addition to the core components mentioned earlier, several scripts and features were
implemented to enhance the functionality and user experience of the project. These
additional implementation details include:

5.6.1 Movement of Dynamic Obstacles:

A* PATHFINDING AGENT- PROJECT REPORT 8


To introduce dynamic obstacles within the game environment, separate scripts were
developed to control their movement. These scripts allowed the dynamic obstacles to
move horizontally (left-right), vertically (up-down), or even rotate around a specific axis.
The movement patterns and parameters for each dynamic obstacle could be customized
to suit the specific requirements of the game or level design.

// Check if the object has reached the target position


if (transform.position == targetPosition)
{
// Toggle the direction
movingToEndPoint = !movingToEndPoint;
}

// Rotate the object around its up axis (Y-axis in Unity) at the specified speed
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);

By incorporating dynamic obstacles, the project provided a more realistic and challenging
environment for the path finding agent. The agent had to adapt its path dynamically to
avoid colliding with these moving obstacles, adding an additional layer of complexity to
the path finding process.

5.6.2 Camera Follow and Zoom Functionality:


To enhance the player's view and provide a more immersive experience, a camera follow
script was implemented. This script ensured that the camera always focused on the main
player or the path finding agent, tracking its movements throughout the game
environment. This allowed the player to have a clear view of the agent's navigation and
the surrounding environment.

private void LateUpdate()


{
// Smoothly move the camera towards the target position
transform.position = Vector3.Lerp(transform.position, targetPosition,
followSpeed * Time.deltaTime);

// Update the camera's orthographic size


cam.orthographicSize = newSize;
}

A* PATHFINDING AGENT- PROJECT REPORT 9


Additionally, the camera script provided the ability to zoom in and out, allowing the player
to adjust the camera's field of view for a better overall perspective. The zoom functionality
further improved the player's control and understanding of the game environment,
providing flexibility to focus on specific areas or details as needed.

5.6.3 Line Renderer for Path Visualization:


To visually depict the path between the path finding agent and its target, a line renderer
component was utilized. The line renderer script drew a line or path in the game scene,
connecting the agent's current position with the goal position. This line visually
represented the path calculated by the A* algorithm, making it easier for the player to
understand and follow the agent's intended route.

The line renderer script was designed to update dynamically as the agent moved or if the
target location changed. This allowed the player to visualize the agent's planned path in
real-time, assisting in decision-making and understanding the path finding process.

isVisible = !isVisible;
lineRenderer.enabled = isVisible;

By incorporating these additional features and scripts, the project provided a more
immersive and interactive experience for the player. The movement of dynamic obstacles
added complexity, the camera follow and zoom functionality improved visibility and
control, and the line renderer enhanced the visual feedback, all contributing to a more
engaging and enjoyable gameplay experience.

6. Results and Evaluation


The pathfinding agent demonstrated remarkable performance in all three levels:

In the first level (Maze), the agent efficiently found the optimal path through the maze,
avoiding walls and obstacles.

A* PATHFINDING AGENT- PROJECT REPORT 10


In the second level (Off-Mesh Links and Obstacles), the agent successfully jumped and
climbed between different surfaces, including stairs and slopes, to reach the destination.

In the third level (Dynamic Obstacles), the agent showcased its ability to dynamically adjust
its path to avoid collisions with moving obstacles while still finding the optimal route to the
goal.

The agent's performance was evaluated based on the following criteria:

Path Efficiency: The agent consistently found the shortest path from the starting point to
the goal, minimizing unnecessary movements and avoiding obstacles.

Real-time Adaptability: The agent effectively adjusted its path in real-time to avoid
dynamic obstacles, ensuring smooth navigation even in a changing environment.

Smooth Movement: The agent moved smoothly and realistically, demonstrating natural
navigation behavior while traversing different surfaces and obstacles.

Overall, the pathfinding agent met the project's objectives by successfully navigating through
different levels and handling various obstacles using the A* algorithm and the Unity Nav Mesh
Agent.

7. Use Cases and Real-World Applications:


The path finding agent developed using the A* algorithm and Unity Nav Mesh Agent has several
potential use cases and can be valuable in various real-world applications. Some of these
applications include:

7.1 Game Development:


The project's primary focus was on game development, where path finding agents are
commonly used. The path finding agent can be utilized to create intelligent non-player
characters (NPCs) that navigate through game environments, avoiding obstacles and
dynamically finding the optimal path. This enhances the realism and immersion of the game,
providing challenging and interactive gameplay experiences for players.

7.2 Virtual Reality (VR) and Augmented Reality (AR):


In VR and AR applications, realistic navigation for virtual avatars or characters is crucial for
creating immersive experiences. The path finding agent developed in this project can be
employed to enable intelligent navigation within virtual environments, allowing users to
interact with virtual objects, explore virtual worlds, or engage in virtual training simulations.

7.3 Robotics and Autonomous Systems:


The A* algorithm and Unity Nav Mesh Agent implementation can be adapted for robotics and
autonomous systems. For instance, in warehouse automation, path finding agents can be
utilized to guide robots in navigating through complex warehouse layouts, optimizing their
movement to increase efficiency in picking and transporting goods.

A* PATHFINDING AGENT- PROJECT REPORT 11


7.4 Urban Planning and Traffic Management:
The project's path finding agent can be applied in urban planning and traffic management
systems. By simulating real-world scenarios and considering factors such as traffic
congestion and road conditions, the agent can assist in optimizing routes for vehicles or
pedestrians. This can contribute to reducing congestion, improving transportation efficiency,
and enhancing urban planning strategies.

7.5 Emergency Response and Evacuation Planning:


During emergency situations, such as natural disasters or building evacuations, the path
finding agent can aid in planning evacuation routes for individuals. By analyzing the
environment and accounting for obstacles, the agent can calculate the fastest and safest
paths, helping to save lives and minimize casualties during critical situations.

7.6 Logistics and Supply Chain Optimization:


In logistics and supply chain management, efficient transportation and route optimization are
crucial for minimizing costs and maximizing productivity. The path finding agent can be
utilized to optimize delivery routes, warehouse management, and resource allocation,
leading to improved operational efficiency and reduced transportation expenses.

7.7 Personal Navigation and GPS Applications:


The path finding agent can also find utility in personal navigation systems and GPS
applications. By integrating the agent into navigation apps, users can receive optimized
routes for reaching their destinations, taking into account real-time traffic conditions, road
closures, and other obstacles.
Overall, the project's path finding agent using the A* algorithm and Unity Nav Mesh Agent
has diverse applications in gaming, virtual reality, robotics, urban planning, emergency
response, logistics, and personal navigation. Its ability to calculate optimal paths, navigate
through complex environments, and avoid obstacles makes it a valuable tool in various real-
world scenarios, contributing to enhanced efficiency, safety, and user experiences.

8. Future Enhancements:
While the project has achieved its intended goals, there are several avenues for future
enhancements and further exploration. Some potential areas for improvement and expansion
include:

A* PATHFINDING AGENT- PROJECT REPORT 12


Implementing more advanced heuristics to enhance the efficiency and accuracy of the path
finding algorithm.

Introducing additional path finding algorithms to compare and evaluate their performance
against the A* algorithm.

Incorporating machine learning techniques to train the path finding agent and enable
adaptive behavior in dynamic environments.

Expanding the game environment with more complex layouts, terrain features, and
interactive elements to provide a richer and more immersive experience.

Integrating multiplayer functionality to allow multiple players to interact and collaborate


within the game environment.

Enhancing the user interface to provide more customization options, visual feedback, and
user-friendly controls.

By pursuing these future enhancements, the project can be further improved and extended,
leading to more advanced and sophisticated path finding systems within Unity-based
applications and beyond.

9. Conclusion
The development of the pathfinding agent using the A* algorithm and Unity Nav Mesh Agent
proved to be a successful project. The agent exhibited impressive pathfinding capabilities in
different scenarios, including basic mazes, off-mesh links, stairs, slopes, and moving obstacles.
The integration of the A* algorithm with the Nav Mesh Agent component provided efficient and
optimized pathfinding functionality, enabling the agent to find the optimal paths while avoiding
obstacles.

The project demonstrated the importance of pathfinding algorithms and navigation systems in
creating intelligent and interactive game environments. The implemented levels showcased the
versatility and adaptability of the pathfinding agent, highlighting its ability to handle complex
situations and find optimal paths.

Future enhancements could include further optimization of the A* algorithm to improve


performance in larger and more complex environments, as well as the addition of more diverse
and challenging levels to push the agent's capabilities even further.

A* PATHFINDING AGENT- PROJECT REPORT 13

You might also like