set can move
This commit is contained in:
parent
3869861355
commit
c18ea77b66
@ -40,6 +40,29 @@ animation_library={
|
|||||||
"animation/fps": 60.0
|
"animation/fps": 60.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[input]
|
||||||
|
|
||||||
|
move_left={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_right={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_up={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_down={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
[rendering]
|
[rendering]
|
||||||
|
|
||||||
renderer/rendering_method="gl_compatibility"
|
renderer/rendering_method="gl_compatibility"
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
[gd_scene load_steps=4 format=3 uid="uid://dj16n8yvwwgr7"]
|
[gd_scene load_steps=5 format=3 uid="uid://dj16n8yvwwgr7"]
|
||||||
|
|
||||||
[ext_resource type="FontFile" uid="uid://ch6s7n1ri81gt" path="res://assets/fonts/思源宋体.TTF" id="1_dg3fm"]
|
[ext_resource type="FontFile" uid="uid://ch6s7n1ri81gt" path="res://assets/fonts/思源宋体.TTF" id="1_dg3fm"]
|
||||||
|
[ext_resource type="Script" uid="uid://b8alcxaoi45xq" path="res://scripts/CharacterBody/CharacterBody.cs" id="1_xfrxc"]
|
||||||
[ext_resource type="Texture2D" uid="uid://dbyovpk0xc6jo" path="res://assets/character/2D雷恩.png" id="2_xfrxc"]
|
[ext_resource type="Texture2D" uid="uid://dbyovpk0xc6jo" path="res://assets/character/2D雷恩.png" id="2_xfrxc"]
|
||||||
|
|
||||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_s5ben"]
|
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_s5ben"]
|
||||||
bg_color = Color(0, 0.701961, 0.54902, 0.784314)
|
bg_color = Color(0, 0.701961, 0.54902, 0.784314)
|
||||||
|
|
||||||
[node name="Rayne" type="CharacterBody2D"]
|
[node name="Rayne" type="CharacterBody2D"]
|
||||||
|
script = ExtResource("1_xfrxc")
|
||||||
|
|
||||||
[node name="ProgressBar" type="ProgressBar" parent="."]
|
[node name="ProgressBar" type="ProgressBar" parent="."]
|
||||||
visible = false
|
visible = false
|
||||||
|
File diff suppressed because one or more lines are too long
62
scripts/CharacterBody/CharacterBody.cs
Normal file
62
scripts/CharacterBody/CharacterBody.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Milimoe.GodotGame
|
||||||
|
{
|
||||||
|
public partial class CharacterBody : CharacterBody2D
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 移动速度 (像素/秒)
|
||||||
|
/// </summary>
|
||||||
|
[Export]
|
||||||
|
public float Speed = 200.0f;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// _PhysicsProcess 是 Godot 进行物理计算和运动更新的函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="delta"></param>
|
||||||
|
public override void _PhysicsProcess(double delta)
|
||||||
|
{
|
||||||
|
// --- 1. 获取输入向量 ---
|
||||||
|
// 获取水平输入轴的值 (-1 到 1)
|
||||||
|
float inputX = Input.GetAxis("move_left", "move_right");
|
||||||
|
// 获取垂直输入轴的值 (-1 到 1)
|
||||||
|
// 你需要在项目设置 -> Input Map 中设置 "move_up" 和 "move_down" 动作,
|
||||||
|
// 并绑定相应的按键(如 W/上箭头 和 S/下箭头)。
|
||||||
|
float inputY = Input.GetAxis("move_up", "move_down");
|
||||||
|
|
||||||
|
// 创建一个表示输入方向的向量
|
||||||
|
Vector2 inputVector = new Vector2(inputX, inputY);
|
||||||
|
|
||||||
|
// --- 2. 计算目标速度 ---
|
||||||
|
Vector2 targetVelocity;
|
||||||
|
|
||||||
|
// 检查是否有输入
|
||||||
|
if (inputVector.LengthSquared() > 0) // 使用 LengthSquared() 比 Length() 性能稍好,只需要判断是否大于0
|
||||||
|
{
|
||||||
|
// 如果有输入,将输入向量标准化 (避免对角线移动过快)
|
||||||
|
// 然后乘以速度得到目标速度
|
||||||
|
targetVelocity = inputVector.Normalized() * Speed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 如果没有输入,目标速度为零 (停止)
|
||||||
|
targetVelocity = Vector2.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 3. 平滑过渡到目标速度 ---
|
||||||
|
// 使用 Mathf.MoveToward 平滑地改变当前速度到目标速度
|
||||||
|
// 这样角色移动和停止会更平滑,而不是瞬间加速/停止
|
||||||
|
// 这里的 Speed 用作最大步长,确保在1秒内能达到最大速度(如果 delta 累积到1)
|
||||||
|
Velocity = Velocity.MoveToward(targetVelocity, Speed * (float)delta);
|
||||||
|
|
||||||
|
// --- 4. 调用 MoveAndSlide() ---
|
||||||
|
// 这是关键步骤!它会根据当前的 Velocity 向量移动 CharacterBody2D,
|
||||||
|
// 并自动处理与场景中其他碰撞体的碰撞。
|
||||||
|
// 在俯视角游戏中,MoveAndSlide() 会处理与墙壁、障碍物等的碰撞,阻止角色穿过它们。
|
||||||
|
MoveAndSlide();
|
||||||
|
|
||||||
|
// --- 5. (可选) 其他逻辑 ---
|
||||||
|
// 你可以在这里添加其他逻辑,比如播放动画、处理交互等。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
scripts/CharacterBody/CharacterBody.cs.uid
Normal file
1
scripts/CharacterBody/CharacterBody.cs.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://b8alcxaoi45xq
|
Loading…
x
Reference in New Issue
Block a user