【问题标题】:Player slides ramps in godot玩家在 godot 中滑坡
【发布时间】:2021-11-04 15:10:32
【问题描述】:

所以我在 Godot 3.3.4 中开发了这款基于 3D 的自上而下的游戏。我为玩家移动编写了一些代码,但它会导致玩家在站在坡道上时缓慢滑动。

我可以想象如何修复它,但我认为我设计了一个非常简单的修复程序,所以我问社区。有没有一种简单的方法可以让玩家在触地时保持原地不动?

(我知道 move_and_slide_with_snap() 方法,但我也无法使其正常工作) (我也知道我忘了应用 delta)

代码如下:

export var speed = 7.5
const GRAVITY = Vector3.DOWN * 100

var velocity = Vector3.ZERO

func _physics_process(delta):
  velocity += GRAVITY * delta
  velocity = move_and_slide(velocity, Vector3.UP)
  
  var input_vector = Vector2.ZERO
  input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
  input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
  input_vector = input_vector.normalized()
  
  velocity.x = input_vector.x * speed
  velocity.z = input_vector.y * speed
  

  move_and_slide(velocity)

这是我的第一个社区问你好,谢谢你。

已解决

我有一些 gif,但显然你需要 10 个代表,而且我是新人所以......

谢谢Theraot,我已经启用了斜坡功能,但有时会起作用。所以我做了一些改变,我认为我可以接受:


extends KinematicBody

export var speed = 750
const GRAVITY = Vector3.DOWN * 10

var velocity = Vector3.ZERO

func _physics_process(delta):
    velocity += GRAVITY * delta
    velocity = move_and_slide_with_snap(velocity, Vector3.DOWN, Vector3.UP, true, 1, 0.80)
    
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    input_vector = input_vector.normalized()
    
    velocity.x = input_vector.x * speed * delta
    velocity.z = input_vector.y * speed * delta
    

    move_and_slide_with_snap(velocity, Vector3.DOWN, Vector3.UP, true, 0, 0.785398)



我确信代码几乎可以工作,但动作还不错,它只会在大坡道上滑动一点,所以这可能是一件“有趣”的事情。

我正在关闭它,但如果有人想更正该代码,我可以接受。玩得开心。

【问题讨论】:

    标签: game-development godot gdscript


    【解决方案1】:

    看看move_and_slide的参数:

    Vector3 move_and_slide ( Vector3 linear_velocity, Vector3 up_direction=Vector3( 0, 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

    上面写着bool stop_on_slope=false。我相信那是你想要的。

    所以你的move_and_slide 电话看起来像这样:

    velocity = move_and_slide(velocity, Vector3.UP, true)
    

    我还想提一下up_directionfloor_max_angle 用于决定什么是地板/坡度。


    附录

    它只会在大坡道上滑动一点

    如果问题是陡峭的斜坡,请尝试增加 floor_max_angle0.785398 的值是一个八圈(TAU * 0.125,当然是弧度)。或者 45 度,如果你愿意的话。 0.8 仅此而已,它是 45.8 度 (TAU * 0.127324)。


    其他的:你正在做两次调用,看起来垂直速度是重力的两倍,至少在空中是这样。想到这让我有了另一个想法。如果您将move_and_collide 用于垂直分量(即仅重力),则不会发生滑动。然后您可以使用move_and_slidemove_and_slide_with_snap 进行其他动作。


    顺便说一句,move_and_slide_with_snap 中的“snap”指的是相关但不相同的东西。

    考虑斜坡上的角色,沿水平方向移动,斜坡下降,但下降速度不够快(因为斜坡太陡,水平速度太大,或者重力太低)。并且没有捕捉,角色在空中飞行。 所以,是的,捕捉很重要,但不一定是滑动的解决方案。

    捕捉对于让角色粘在墙上也很有用。例如墙上跑步。

    什么决定什么是地板,什么是墙壁? up_directionfloor_max_angle。可能是您的角色“滑动了一点”,因为坡道不算作地板,而是算作墙壁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多