And yes, further links to subsequent articles will be provided.
Base Code
In order for the character to obey physics, you need to call move_and_slide () in the _process () or _physics_process () method.
Throughout the following code, I will use static typing.
extends KinematicBody2D
# gdscript , python
var velocity: Vector2 = Vector2.ZERO # .
#
#
func _physics_process(_delta: float) -> void:
#
self.velocity = self.move_and_slide(self.velocity, Vector2(0, -1))
# .
# - , .
# .
This code will be enough to make any KinematicBody2D object move depending on collisions with other objects, but it is impossible to control this object, especially in platformers this object still cannot fall down. In order for the object to start falling, you need to increase the value of self.velocity.y by a positive value. The fact is that Godot Engine calculates both coordinates from the upper left corner to the lower right one in 2D mode. In order for the object to fall, you need to add something to the acceleration. I usually use the GRAVITY constant, which is given at the beginning of the program. Next, the code will be presented with changes in order for the object to fall.
extends KinematicBody2D
const GRAVITY: int = 40
# 40 ,
var velocity: Vector2 = Vector2.ZERO #
func _physics_process(_delta: float) -> void:
#
#
self.velocity.y += GRAVITY
# _delta , self.move_and collide
self.velocity = self.move_and_slide(self.velocity, Vector2(0, -1))
# , . .
Move control should be implemented in a separate method, so as not to complicate 1 method too much. Moreover, no one can withstand the repetition of the same code in one article 33 times.
Moving
I recently learned one interesting way to control a character, which became an alternative to the first method I mastered. Next, I will show both methods:
- The first, new way for me
func move_character() -> void: var direction: float = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left") # # (+ x, 0 1) (- x, 0 1). # = 0, < 0 , # , . self.velocity.x = direction * MOVE_SPEED # . # const MOVE_SPEED = # const GRAVITY
- Second way
func move_character() -> void: # var direction: float = 0 if Input.is_action_pressed("ui_left"): direction = -1 elif Input.is_action_pressed("ui_right"): direction = 1 else: direction = 0 self.velocity.x = direction * MOVE_SPEED # const MOVE_SPEED = # # const GRAVITY
Bounce
The jump is realized almost as easily and unambiguously as the first method of movement. To do this, you need to create a jump force constant, as the speed of movement at the beginning, and set a value greater than the value of gravity. Let's go straight to the program code:
func jump() -> void:
if self.is_on_floor(): # , .
# ,
if Input.is_action_pressed("ui_jump"): #
# ui_jump .
# "ui_up"
self.velocity.y -= JUMP_POWER
# const JUMP_POWER...
It remains only to draw a map, add collisions and other things that are not directly related to the main code and for the game character it will be possible to finally arrange a run through the locations, but about this still another time, because so far I have not figured out how to explain other code fragments more correctly , and this is beyond the scope of the message.