Unity's solution based on group pathfinding

Crowd pathfinding is a technique for simulating crowd behavior that allows characters in a game to move in an organized fashion. In a swarm, each character has his own target location and will decide how to move based on the surrounding situation. Group pathfinding can help us realize some interesting scenes, such as flocks of birds flying, schools of fish swimming, etc.

right! There is a game development exchange group here, which gathers a group of zero-based beginners who love to learn games, and there are also some technical experts who are engaged in game development. You are welcome to exchange and learn.

To implement group pathfinding in Unity, we can use the NavMeshAgent component. NavMeshAgent is a component used for pathfinding in Unity, which can automatically calculate the movement path of the character and control the movement of the character. To use the NavMeshAgent component, we need to first create a NavMesh and then apply it to the ground in the scene.

First, we need to create an empty object and name it "NavMesh". Then, we need to create a ground in the scene for the character's movement. Right click on the ground, select "Navigation", then click "Create NavMesh". This will generate a NavMesh for the ground, used for pathfinding.

Next, we need to add the NavMeshAgent component to the character. Select a character in the scene, then click "Add Component", select "Navigation", and click "NavMeshAgent". This will add a NavMeshAgent component on the character.

Now, we can control the character's movement through code. First, we need to get a reference to the NavMeshAgent component in the code. We can use the GetComponent method to get a reference to the NavMeshAgent component as follows:

NavMeshAgent agent = GetComponent<NavMeshAgent>();

Next, we can use the SetDestination method to set the character's destination. For example, we can set the target position of the character to the position of the mouse click like this:

if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit))
{
    agent.SetDestination(hit.point);
}

}

The above code will get the position of the mouse click when the left mouse button is clicked and set it as the target position of the character.

In addition to setting the target position, we can also set the movement speed and turning speed of the character. We can use the agent.speed property to set the movement speed and the agent.angularSpeed ​​property to set the turning speed. For example, we can set the movement speed to 5 and the turning speed to 120 as follows:

agent.speed = 5;
agent.angularSpeed = 120;

We have now completed the code implementation of our swarm pathfinding based solution. When we run the game, we can see the characters move intelligently based on where the mouse is clicked, avoiding collisions and interfering with each other.

Guess you like

Origin blog.csdn.net/voidinit/article/details/131560191