我对编程很陌生,也是一个使用教程为我的第一个游戏创建机芯的人。它可以很好地移动,除了角色会突然停止。当没有按下输入时,有没有办法慢慢降低速度。类似于地震。
direction.z = Input.get_action_strength("move_backward")-Input.get_action_strength("move_forward")
direction = direction.normalized()
direction = direction.rotated(Vector3.UP,rotation.y)
velocity.x = direction.x * movementSpeed
velocity.z = direction.z * movementSpeed
move_and_slide(velocity,Vector3.UP)
https://youtu.be/vEuR-Bo0feY这是的参考视频
获取move_and_slide
返回的速度:
velocity = move_and_slide(velocity,Vector3.UP)
这是处理滑动和碰撞后的速度。我们将减少它:
velocity -= velocity * min(delta/stop_time, 1.0)
其中,stop_time
是一个变量,它控制你希望它以秒为单位停止的速度(与delta)
的单位相同。因此,如果是stop_time = 1.0
,它将在一秒钟后停止。将其调整为你想要的值。如果你愿意,你也可以硬编码该值,但我建议使用一个变量。
现在,我们的想法是,我们将在下一次运行_physics_process
时保持速度,并且只有在有输入时才将其设置为完整的movementSpeed
。因此,您可以检查direction
是否为零,如果为零,则不要更改velocity
。
类似这样的东西:
if direction.z != 0:
direction = direction.normalized()
direction = direction.rotated(Vector3.UP,rotation.y)
velocity.x = direction.x * movementSpeed
velocity.z = direction.z * movementSpeed
velocity = move_and_slide(velocity,Vector3.UP)
velocity -= velocity * min(delta/stop_time, 1.0)
顺便说一句,如果你也要增加重力,我想你不想摔倒也减速。因此,您可能只想将其应用于x
和z
。好消息是,你可以按组件进行:
if direction.z != 0:
direction = direction.normalized()
direction = direction.rotated(Vector3.UP,rotation.y)
velocity.x = direction.x * movementSpeed
velocity.z = direction.z * movementSpeed
velocity = move_and_slide(velocity,Vector3.UP)
var slowdown := min(delta/stop_time, 1.0)
velocity.x -= velocity.x * slowdown
velocity.z -= velocity.z * slowdown
顺便说一句,当应用于速度时,将重力乘以delta
你可能不想要线性减速,而是想要一些自定义的放松。你可以这样完成:
var slowdown := ease(min(delta/stop_time, 1.0), easing_curve)
其中easing_curve
定义了用于缓和的曲线,请参阅备忘单。
对于更复杂的行为,您需要使用Tween