时间:2023-06-26 23:45:01 | 来源:网站运营
时间:2023-06-26 23:45:01 来源:网站运营
如何制作移动的平台-Godot3-2D教程:onready var platform = $Platformonready var tween = $MoveTween
接着,我们要定义3个变量:export var move_to = Vector2(100, 0)export var speed = 50.0var delay = 1.0
move_to 和 speed 使用了 export ,目的是方便在属性里设置。bool interpolate_property(object: Object, property: NodePath, initial_val: Variant, final_val: Variant, duration: float, trans_type: TransitionType = 0, ease_type: EaseType = 2, delay: float = 0)
Animates property of object from initial_val to final_val for duration seconds, delay seconds later. Setting the initial value to null uses the current value of the property.
Use TransitionType for trans_type and EaseType for ease_type parameters. These values control the timing and direction of the interpolation. See the class description for more information.
func _ready(): var duration = move_to.length() / speed tween.interpolate_property(platform, "position", Vector2.ZERO, move_to, duration, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT, delay) tween.start()
保存后,将 MovingPlatform 场景拖入主场景中,运行测试。func _ready(): var duration = move_to.length() / speed tween.interpolate_property(platform, "position", Vector2.ZERO, move_to, duration, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT, delay) tween.interpolate_property(self, "follow", move_to, Vector2.ZERO, duration, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT, duration + delay * 2) tween.start()
注意:两个interpolate_property方法,并不是一个执行完另一个再执行,它们会一起运行,错开它们的就靠 delay 参数。意思是第一个方法将平台移动到位置后,第二个方法因为delay参数才开始执行,所以,只要将delay时间设置好,它就总是能在正确的时间去执行。Vector2 move_and_slide_with_snap(linear_velocity: Vector2, snap: Vector2, up_direction: Vector2 = Vector2( 0, 0 ), stop_on_slope: bool = false, max_slides: int = 4, floor_max_angle: float = 0.785398, infinite_inertia: bool = true)可以看到,它比前者多了一个参数: snap 。它的作用是用于检测目村向量范围内是否有地面,如果有,就吸住它,从而解决抖动问题。因此替换Player的移动代码:
Moves the body while keeping it attached to slopes. Similar to move_and_slide().
As long as the snap vector is in contact with the ground, the body will remain attached to the surface. This means you must disable snap in order to jump, for example. You can do this by setting snap to (0, 0) or by using move_and_slide() instead.
var snap = Vector2.ZERO if is_jumping else Vector2.DOWN * 16velocity = move_and_slide_with_snap(velocity, snap, Vector2.UP)
snap值根据是否跳跃进行判断,如果在跳跃,那么值为ZERO,意味着不要吸住,不然就跳不起来了。如果不在跳跃,就吸住,而检测的距离为Vector2.DOWN * 16,也 就是向下乘16px。var floor_velectiry_x = 0
接着在按下跳跃键的位置,添加获取平台移动速度的代码:if Input.is_action_just_pressed("jump") and !is_jumping: velocity.y -= 500 if get_floor_velocity().x != 0: floor_velectiry_x = get_floor_velocity().x
最后在移动前,将它赋给角色:if floor_velectiry_x != 0: velocity.x += floor_velectiry_x
保存运行测试:关键词:平台,教程,移动