Godot2D角色导航-自动寻路教程(Godot实现角色随鼠标移动)

文章目录

运行结果

2D导航概述

Godot为2D和3D游戏提供了多个对象、类和服务器,以便于基于网格或基于网格的导航和路径查找。

说到导航,就得说一下导航网格,导航网格定义了角色可以站立和移动的区域,以其中心为基准。

本文章内容主要以官方文档为主,链接如下:官方文档导航部分

开始前的准备

为你的项目设置合适的窗口大小,如下图所示,宽度和高度自定义。

2D导航

创建导航网格

接下来,我们就创建一个导航网格。

首先为你的场景添加一个地图,也就是玩家可以走的路,这里随便弄,如下图所示。

接下来添加一个NavigationRegion2D组件。

单击该组件,在检查器窗口中,新建一个Navigation Polygon。

接着,点击该属性,在场景中绘制你的导航网格,如下图所致。连接第一个点和最后一个点可以完成导航网格的绘制。

注意在导航多边形的边缘和碰撞对象之间留出足够的间距,以避免路径跟随的角色在碰撞中反复被卡住。

创建角色

创建一个CharacterBody2D节点。为其添加Sprite2D子节点,为Sprite2D的Texture属性赋值,给他弄一个角色图片,任意即可。为CharacterBody2D添加CollisionShape2D子节点,为其Shape属性添加一个形状。调整大小以契合Sprite2D。

然后为CharacterBody2D添加NavigationAgent2D节点,最后结果如下图所示:

为CharacterBody2D节点添加一个MyCharacterBody2D脚本,并为其编写如下内容:

csharp 复制代码
using Godot;

public partial class MyCharacterBody2D : CharacterBody2D
{
	private NavigationAgent2D _navigationAgent;

	private float _movementSpeed = 200.0f;
	private Vector2 _movementTargetPosition = new Vector2(500.0f, 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;

		// Make sure to not await during _Ready.
		Callable.From(ActorSetup).CallDeferred();
	}
    public override void _Process(double delta)
    {
        base._Process(delta);
		_movementTargetPosition=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();
	}

	private async void ActorSetup()
	{
		// Wait for the first physics frame so the NavigationServer can sync.
		await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);

		// Now that the navigation map is no longer empty, set the movement target.
		MovementTarget = _movementTargetPosition;
	}
	public Vector2 GetMouseClickPosition()
	{
		Vector2 mousePosition = GetGlobalMousePosition();
		return mousePosition;
	}
}

最终运行结果如下图所示:

接下来我们在详细讲解一下代码,文章连接如下:
设置目标位置

其他文章

Godot实现闪烁效果
Godot信号教程

相关推荐
ysdysyn4 分钟前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..3 小时前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
wanhengidc3 小时前
云手机的魅力与优势
网络·游戏·智能手机·架构·云计算
reasonsummer4 小时前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
小L~~~16 小时前
2025吉比特-游戏引擎开发-一面复盘
数据结构·算法·游戏引擎
AI广告侠18 小时前
游戏开发者的AI革命:从灵感枯竭到效率狂飙,我的日常链路被AI重塑了 ——一个独立游戏开发者的AI工具箱实战指南
游戏·游戏开发
Hello123网站19 小时前
h5游戏免费下载:一起蛙蛙跳
游戏
William_cl20 小时前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
@sinner21 小时前
《扫雷:病毒蔓延》- 颠覆传统的动态扫雷游戏
python·游戏·pygame
云卓SKYDROID1 天前
无人机中继器模式技术对比
人工智能·游戏引擎·php·无人机·cocos2d·高科技·云卓科技