Gideros endless runner5/16/2023 ![]() You should see a penguin slowly falling, and when you press spacebar, you should see the penguin flap. ![]() This code makes it so that when spacebar is pressed, the penguin flaps! Try this out using the Cmd R keyboard shortcut, which plays the scene you’re currently editing (this should be the penguin scene). The script will contain this code: extends RigidBody2D const UP_IMPULSE: float = -55.0 func _ready() -> void: pass func _input(event: InputEvent) -> void: if event is InputEventKey: if event.is_action_pressed("ui_select"): _penguin_jump() func _penguin_jump() -> void: set_linear_velocity(Vector2(0, 0)) apply_central_impulse(Vector2(0, UP_IMPULSE)) $FlapAnimationPlayer.stop() $ay("Flap", -1, 1) Now to test out our animation, we’re going to attach a script to the root node of our penguin scene called Penguin.gd. The “Flap” animation should look like this. The first frame will have Frame = 1 and the second frame will have Frame = 0. Our “Flap” animation will have two frames, one at 0 seconds and one at 0.4 seconds. We can use these keys to add frames to our animation. Since we have an AnimationPlayer in the scene, we can see these little keys next to fields in the Inspector. We’ll add a Property Track that animates the Frame field of the Sprite node. Now, let’s go to our AnimationPlayer and create a new animation called “Flap”. Note that Penguin.png is a sprite sheet we’re going to be animating the Frame field of the Sprite to create the flapping animation. Drag Penguin.png into the Texture field of the Sprite, and set Hframes = 3. Then, add a Sprite and an AnimationPlayer as children. To start, create a RigidBody2D as the root node of a new scene. A RigidBody2D should usually be combined with some kind of collision shape (Godot will warn you if it’s not).įirst, we’ll add the flapping animation. Without this, our penguin won’t collide with anything.
0 Comments
Leave a Reply. |