Debian Codegraph @

modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs

raw symbols SCIP
1 // meta-description: Classic movement for gravity games (platformer, ...)
2
3 using _BINDINGS_NAMESPACE_;
4 using System;
5
6 public partial class _CLASS_ : _BASE_
7 {
8 public const float Speed = 300.0f;
9 public const float JumpVelocity = -400.0f;
10
11 public override void _PhysicsProcess(double delta)
12 {
13 Vector2 velocity = Velocity;
14
15 // Add the gravity.
16 if (!IsOnFloor())
17 {
18 velocity += GetGravity() * (float)delta;
19 }
20
21 // Handle Jump.
22 if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
23 {
24 velocity.Y = JumpVelocity;
25 }
26
27 // Get the input direction and handle the movement/deceleration.
28 // As good practice, you should replace UI actions with custom gameplay actions.
29 Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
30 if (direction != Vector2.Zero)
31 {
32 velocity.X = direction.X * Speed;
33 }
34 else
35 {
36 velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
37 }
38
39 Velocity = velocity;
40 MoveAndSlide();
41 }
42 }