Godot4 3D游戏实战:从零搭建一个能跳能踩、有分数有音乐的完整Demo

Godot4 3D游戏实战:从零搭建一个能跳能踩、有分数有音乐的完整Demo Godot4 3D游戏开发实战构建跳跃踩怪完整Demo从零开始的3D游戏开发之旅当你第一次打开Godot引擎时可能会被它简洁的界面和强大的功能所震撼。作为一款开源游戏引擎Godot近年来在独立游戏开发者中获得了极高的声誉特别是其第四代版本引入了全新的3D渲染管线和完善的脚本系统。本教程将带你从零开始构建一个完整的3D平台跳跃游戏Demo包含角色控制、怪物生成、分数系统和视听反馈等核心游戏机制。这个Demo将实现一个经典的游戏玩法玩家控制角色在平台上移动跳跃通过踩踏怪物获得分数同时要避免被怪物碰到。虽然看起来简单但其中包含了3D游戏开发的多个关键概念物理系统、动画控制、场景管理、UI交互等。我们将使用Godot4的GDScript进行开发这是一种专为游戏设计的高效脚本语言语法类似Python但针对游戏开发做了优化。1. 项目设置与基础场景搭建1.1 初始化项目与窗口设置启动Godot4后创建一个新项目并选择3D模板。在项目设置中我们需要调整几个关键参数# 项目设置 - 显示 - 窗口 window_width 720 window_height 540 window_resizable false这种较小的窗口尺寸适合我们的简单Demo也便于调试。接下来创建主场景(Main.tscn)它将包含游戏的所有核心元素。1.2 构建基础3D环境我们的游戏需要几个基本3D元素地面(Ground)使用StaticBody3D节点加上MeshInstance3D(网格实例)作为子节点赋予一个平面网格(PlaneMesh)玩家(Player)创建一个新的场景使用CharacterBody3D作为根节点摄像机(Camera3D)设置适当的位置和角度确保能看到整个游戏区域# 地面物理材质设置 var ground_material PhysicsMaterial.new() ground_material.bounce 0.1 ground_material.friction 1.0 $Ground/CollisionShape3D.physics_material_override ground_material1.3 玩家角色基础设置玩家角色是游戏的核心我们需要为其添加几个关键组件碰撞形状CapsuleShape3D适合人形角色视觉表现简单的MeshInstance3D或导入的3D模型脚本控制附加Player.gd脚本处理移动逻辑# Player.gd基础设置 extends CharacterBody3D export var speed 14.0 export var jump_impulse 20.0 export var fall_acceleration 75.0 var target_velocity Vector3.ZERO2. 核心游戏机制实现2.1 玩家移动与跳跃控制3D平台游戏的核心是流畅的移动体验。我们需要处理几个关键输入水平移动(前后左右)跳跃重力模拟func _physics_process(delta): # 获取输入方向 var input_dir Input.get_vector(move_left, move_right, move_forward, move_back) var direction (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() # 水平速度 if direction: target_velocity.x direction.x * speed target_velocity.z direction.z * speed else: target_velocity.x move_toward(target_velocity.x, 0, speed) target_velocity.z move_toward(target_velocity.z, 0, speed) # 垂直速度(重力) if not is_on_floor(): target_velocity.y - fall_acceleration * delta # 跳跃 if is_on_floor() and Input.is_action_just_pressed(jump): target_velocity.y jump_impulse velocity target_velocity move_and_slide()2.2 怪物生成系统怪物是游戏的主要互动元素我们需要实现怪物场景(Mob.tscn)创建生成路径设置定时生成逻辑# Main.gd中的怪物生成逻辑 extends Node export var mob_scene: PackedScene export var spawn_interval 0.5 func _ready(): $MobTimer.wait_time spawn_interval $MobTimer.start() func _on_mob_timer_timeout(): var mob mob_scene.instantiate() var spawn_location $SpawnPath/SpawnLocation spawn_location.progress_ratio randf() mob.initialize(spawn_location.position, $Player.position) add_child(mob)怪物行为脚本需要处理移动和与玩家的交互# Mob.gd基础设置 extends CharacterBody3D export var min_speed 10 export var max_speed 18 signal squashed func initialize(start_position, player_position): look_at_from_position(start_position, player_position, Vector3.UP) rotate_y(randf_range(-PI/4, PI/4)) var random_speed randi_range(min_speed, max_speed) velocity Vector3.FORWARD * random_speed velocity velocity.rotated(Vector3.UP, rotation.y) func _physics_process(delta): move_and_slide() func squash(): squashed.emit() queue_free()2.3 踩踏机制实现踩踏是游戏的核心玩法需要精确的碰撞检测设置物理层和遮罩检测碰撞方向和类型处理踩踏和反弹# Player.gd中的踩踏检测 func _physics_process(delta): # ...之前的移动代码... # 碰撞检测 for index in get_slide_collision_count(): var collision get_slide_collision(index) if collision.get_collider().is_in_group(mob): if Vector3.UP.dot(collision.get_normal()) 0.1: collision.get_collider().squash() target_velocity.y bounce_impulse break3. 游戏状态与UI系统3.1 分数系统实现分数系统需要UI显示元素分数变量跟踪怪物踩踏事件响应# ScoreLabel.gd extends Label var score 0 func _on_mob_squashed(): score 1 text Score: %s % score在主场景中连接信号# Main.gd func _on_mob_timer_timeout(): # ...生成怪物代码... mob.squashed.connect($UserInterface/ScoreLabel._on_mob_squashed)3.2 游戏结束与重启玩家死亡时需要停止怪物生成显示重试UI处理重新开始输入# Player.gd signal hit func die(): hit.emit() queue_free() func _on_mob_detector_body_entered(body): die() # Main.gd func _on_player_hit(): $MobTimer.stop() $UserInterface/Retry.show() func _unhandled_input(event): if event.is_action_pressed(ui_accept) and $UserInterface/Retry.visible: get_tree().reload_current_scene()4. 视听效果增强4.1 角色动画制作Godot的动画系统非常强大创建AnimationPlayer节点设计浮动动画代码控制动画速度# 浮动动画控制 func _physics_process(delta): # ...移动代码... if direction ! Vector3.ZERO: $AnimationPlayer.speed_scale 4 else: $AnimationPlayer.speed_scale 1 # 跳跃弧线 $Pivot.rotation.x PI / 6 * velocity.y / jump_impulse4.2 音效与背景音乐音频系统可以大大增强游戏体验创建自动加载的音乐播放器添加背景音乐游戏音效# 创建MusicPlayer场景并设置为自动加载 # 在项目设置 - 自动加载中添加MusicPlayer.tscn # MusicPlayer.gd extends AudioStreamPlayer func _ready(): stream load(res://assets/HouseInAForestLoop.ogg) play()5. 项目优化与扩展思路5.1 性能优化技巧3D游戏尤其需要注意性能使用可见性通知器(VisibleOnScreenNotifier3D)优化物理计算合理使用实例化# Mob.gd中添加屏幕退出检测 func _ready(): $VisibleOnScreenNotifier3D.screen_exited.connect(queue_free)5.2 游戏设计扩展方向完成基础Demo后可以考虑添加多种怪物类型实现关卡系统加入道具和能力升级设计更复杂的场景# 扩展怪物类型的简单实现 enum MobType {NORMAL, FAST, HEAVY} export var type: MobType MobType.NORMAL func initialize(start_position, player_position): # ...原有代码... match type: MobType.FAST: speed * 1.5 MobType.HEAVY: scale * 1.5开发心得与实用技巧在实际开发过程中有几个关键点值得注意物理层设置合理配置碰撞层和遮罩可以避免很多奇怪的物理问题信号系统Godot的信号机制非常强大应该充分利用它来解耦游戏逻辑调试技巧使用print()输出和调试器可以快速定位问题资源管理保持项目文件结构清晰方便后期维护# 调试用打印示例 func _on_mob_timer_timeout(): print(生成怪物 at: , OS.get_time()) # ...生成代码...完成这个项目后你将掌握Godot4 3D游戏开发的核心工作流程。从场景搭建到脚本编写从物理系统到UI交互这些技能是构建更复杂游戏的基础。最重要的是通过实践理解游戏引擎如何将各种系统组合成一个完整的互动体验。