You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Godot代码实现Shift键触发run动画并提升1.5倍速度?

解决Godot CharacterBody2D角色奔跑功能的修改方案

直接看修改后的代码,关键改动已标注:

extends CharacterBody2D

var speed := 120
var direccion := 0.0 
var jump := 250
var gravity := 9

@onready var anim := $AnimationPlayer
@warning_ignore("unused_parameter")
func _physics_process(delta):
    direccion = Input.get_axis("ui_left","ui_right")
    # 检测是否按住Shift键(对应输入动作"ui_shift")
    var is_running = Input.is_action_pressed("ui_shift")
    
    # 根据奔跑状态计算移动速度
    velocity.x = direccion * speed * (is_running ? 1.5 : 1)
    
    # 调整动画播放逻辑
    if direccion != 0.0:
        if is_running:
            anim.play("run")
        else:
            anim.play("walk")
    else:
        anim.play("idle")
    
    if is_on_floor() and Input.is_action_just_pressed("ui_accept"):
        velocity.y -= jump
        
    if !is_on_floor():
        velocity.y += gravity
    
    move_and_slide()

关键改动说明:

  • 新增is_running变量,通过Input.is_action_pressed("ui_shift")检测Shift键是否按住(如果你的项目里Shift对应的输入动作不是"ui_shift",需改成自己设置的动作名)
  • 计算水平速度时,根据is_running状态乘以1.5倍系数
  • 调整动画分支逻辑:有移动方向时,优先判断是否在奔跑,播放对应的"run"或"walk"动画;无方向时保持播放"idle"

内容的提问来源于stack exchange,提问作者Carlos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 16:12:36