時間:2023-06-26 23:45:01 | 來源:網(wǎng)站運營
時間:2023-06-26 23:45:01 來源:網(wǎng)站運營
如何制作移動的平臺-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 ,目的是方便在屬性里設(shè)置。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方法,并不是一個執(zhí)行完另一個再執(zhí)行,它們會一起運行,錯開它們的就靠 delay 參數(shù)。意思是第一個方法將平臺移動到位置后,第二個方法因為delay參數(shù)才開始執(zhí)行,所以,只要將delay時間設(shè)置好,它就總是能在正確的時間去執(zhí)行。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)可以看到,它比前者多了一個參數(shù): snap 。它的作用是用于檢測目村向量范圍內(nèi)是否有地面,如果有,就吸住它,從而解決抖動問題。因此替換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值根據(jù)是否跳躍進行判斷,如果在跳躍,那么值為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
保存運行測試:關(guān)鍵詞:平臺,教程,移動
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。