Godot2D Character Navigation - Automatic Pathfinding Tutorial (Godot enables characters to move with the mouse)

operation result

Insert image description here

2D Navigation Overview

Godot provides multiple objects, classes, and servers for 2D and 3D games to facilitate grid-based or grid-based navigation and path finding.

Speaking of navigation, we have to talk about the navigation grid. The navigation grid defines the area where the character can stand and move, based on its center.

The content of this article is mainly based on official documents. The link is as follows:Official document navigation section

Preparation before starting

Set the appropriate window size for your project, as shown in the image below, with custom width and height.
Insert image description here

2D navigation

Create navigation grid

Next, we create a navigation grid.
First add a map to your scene, which is the path that players can take. You can do it here as shown in the figure below.
Insert image description here
Next add a NavigationRegion2D component.
Insert image description here
Click the component and create a new Navigation Polygon in the inspector window.
Insert image description here
Next, click on this attribute to draw your navigation grid in the scene, as shown below. Connecting the first point and the last point completes the drawing of the navigation mesh.
Insert image description here

Be careful to leave enough spacing between the edges of the navigation polygon and the collision object to avoid the path-following character getting stuck repeatedly in collisions.

Creating a Role

Creates a CharacterBody2D node. Add a Sprite2D child node to it, assign a value to the Texture attribute of Sprite2D, and give it a character picture. You can do whatever you want. Add a CollisionShape2D subnode to CharacterBody2D and add a shape to its Shape property. Resize to fit Sprite2D.
Insert image description here
Then add the NavigationAgent2D node to CharacterBody2D. The final result is as shown below:
Insert image description here
Add a MyCharacterBody2D script to the CharacterBody2D node and write the following content for it:

using Godot;

public partial class MyCharacterBody2D : CharacterBody2D
{
    
    
	private NavigationAgent2D _navigationAgent;

	private float _movementSpeed = 200.0f;

	public Vector2 MovementTarget
	{
    
    
		get {
    
     return _navigationAgent.TargetPosition; }
		set {
    
     _navigationAgent.TargetPosition = value; }
	}

	public override void _Ready()
	{
    
    
		base._Ready();
		_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");

		// These values need to be adjusted for the actor's speed
		// and the navigation layout.
		_navigationAgent.PathDesiredDistance = 4.0f;
		_navigationAgent.TargetDesiredDistance = 4.0f;
	}
    public override void _Process(double delta)
    {
    
    
        base._Process(delta);
		MovementTarget =GetMouseClickPosition();
    }

    public override void _PhysicsProcess(double delta)
	{
    
    
		base._PhysicsProcess(delta);

		if (_navigationAgent.IsNavigationFinished())
		{
    
    
			return;
		}

		Vector2 currentAgentPosition = GlobalTransform.Origin;
		Vector2 nextPathPosition = _navigationAgent.GetNextPathPosition();

		Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
		MoveAndSlide();
	}
	public Vector2 GetMouseClickPosition()
	{
    
    
		Vector2 mousePosition = GetGlobalMousePosition();
		return mousePosition;
	}
}

The final running result is shown in the figure below:
Insert image description here
Next we will explain the code in detail. The article link is as follows:
Set the target position

Other articles

Godot achieves flashing effect
Godot signal tutorial

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/133718685