如何像超级马里奥一样设计游戏中的跳跃?

问题描述 投票:0回答:1

我在设计角色的移动和跳跃时,想要实现类似《超级马里奥》中的跳跃效果,但我不知道如何实现这个效果。我尝试了几次,但无法获得正常跳跃的感觉。

这是我的一些代码,可以实现类似的效果,但跳跃并不那么自然。

const SPEED = 300.0
const JUMP_VELOCITY = -300.0
const MAX_JUMP_FORCE = -600.0 
const MAX_JUMP_TIME = 0.5  
var jump_press_duration = 0.0 

func _physics_process(delta):
    print_debug(velocity.y + jump_press_duration * gravity)
    
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta

    ## Handle jump.
    #if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        #velocity.y = JUMP_VELOCITY
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY
        jump_press_duration = 0  
        
    
    if Input.is_action_pressed("ui_accept") and not is_on_floor():
        jump_press_duration += delta
        if jump_press_duration < MAX_JUMP_TIME:
            velocity.y += (MAX_JUMP_FORCE - JUMP_VELOCITY) * (delta / MAX_JUMP_TIME)
    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()
game-physics godot gdscript godot4
1个回答
0
投票

看一下这个问题: 戈多4跳跃不行,看起来更像是传送而不是跳跃

用户使用教程,它可以处理像素完美的跳跃,这与原始的马里奥游戏类似。

© www.soinside.com 2019 - 2024. All rights reserved.