diff --git a/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd b/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd index bd4816827fdd..f60290142853 100644 --- a/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd +++ b/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd @@ -4,24 +4,39 @@ extends _BASE_ const SPEED = 300.0 +const ACCEL = 300.0 + const JUMP_VELOCITY = -400.0 func _physics_process(delta: float) -> void: - # Add the gravity. + # Handle gravity. if not is_on_floor(): velocity += get_gravity() * delta + # Get the vertical velocity (for jump). + var vertical_velocity: Vector2 = velocity.project(up_direction) + + # Get the horizontal velocity (for acceleration). + var horizontal_velocity: Vector2 = velocity - vertical_velocity + # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var direction := Input.get_axis("ui_left", "ui_right") - if direction: - velocity.x = direction * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) + vertical_velocity = up_direction * JUMP_VELOCITY + + # Get the input axis. As good practice, you should replace UI actions with custom gameplay actions. + var input_axis: float = Input.get_axis("ui_left", "ui_right") + + # Calculate the intended direction in 3D space. + var input_direction: Vector2 = Vector2(input_axis, 0).rotated(rotation) + + # Calculate the target horizontal velocity. + var target_horizontal_velocity: Vector2 = input_direction * SPEED + + # Move the current horizontal velocity towards the target horizontal velocity by the acceleration factor multiplied by delta. + horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, ACCEL * delta) + + # Compose the final velocity. + velocity = horizontal_velocity + vertical_velocity move_and_slide() diff --git a/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd b/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd index f9c4f70a24c0..7dbe1c13fedc 100644 --- a/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd +++ b/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd @@ -4,27 +4,38 @@ extends _BASE_ const SPEED = 5.0 +const ACCEL = 5.0 const JUMP_VELOCITY = 4.5 func _physics_process(delta: float) -> void: - # Add the gravity. + # Handle gravity. if not is_on_floor(): velocity += get_gravity() * delta + # Get the vertical velocity (for jump). + var vertical_velocity: Vector3 = velocity.project(up_direction) + + # Get the horizontal velocity (for acceleration). + var horizontal_velocity: Vector3 = velocity - vertical_velocity + # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") - var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() - if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - velocity.z = move_toward(velocity.z, 0, SPEED) + vertical_velocity = up_direction * JUMP_VELOCITY + + # Get the 2D input vector. As good practice, you should replace UI actions with custom gameplay actions. + var input_vector := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down", 0.15) + + # Calculate the intended direction in 3D space. + var input_direction: Vector3 = transform.basis.orthonormalized() * Vector3(input_vector.x, 0, input_vector.y) + + # Calculate the target horizontal velocity. + var target_horizontal_velocity: Vector3 = input_direction * SPEED + + # Move the current horizontal velocity towards the target horizontal velocity by the acceleration factor multiplied by delta. + horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, ACCEL * delta) + + # Compose the final velocity. + velocity = horizontal_velocity + vertical_velocity move_and_slide() diff --git a/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs index 698157c6b4de..a070f5d1bf42 100644 --- a/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs +++ b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs @@ -3,40 +3,50 @@ using _BINDINGS_NAMESPACE_; using System; -public partial class _CLASS_ : _BASE_ +public partial class CharacterBody2d : CharacterBody2D { - public const float Speed = 300.0f; - public const float JumpVelocity = -400.0f; - - public override void _PhysicsProcess(double delta) - { - Vector2 velocity = Velocity; - - // Add the gravity. - if (!IsOnFloor()) - { - velocity += GetGravity() * (float)delta; - } - - // Handle Jump. - if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) - { - velocity.Y = JumpVelocity; - } - - // Get the input direction and handle the movement/deceleration. - // As good practice, you should replace UI actions with custom gameplay actions. - Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); - if (direction != Vector2.Zero) - { - velocity.X = direction.X * Speed; - } - else - { - velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed); - } - - Velocity = velocity; - MoveAndSlide(); - } + public const float Speed = 300.0f; + public const float Accel = 300.0f; + public const float JumpVelocity = -400.0f; + + public override void _PhysicsProcess(double delta) + { + Vector2 velocity = Velocity; + + // Handle gravity. + if (!IsOnFloor()) + { + velocity += GetGravity() * (float)delta; + } + + // Get the vertical velocity (for jump). + Vector2 verticalVelocity = velocity.Project(UpDirection); + + // Get the horizontal velocity (for acceleration). + Vector2 horizontalVelocity = velocity - verticalVelocity; + + // Handle Jump. + if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + { + verticalVelocity = UpDirection * JumpVelocity; + } + + // Get the input axis. As good practice, you should replace UI actions with custom gameplay actions. + float inputAxis = Input.GetAxis("ui_left", "ui_right"); + + // Calculate the intended direction in 3D space. + Vector2 inputDirection = new Vector2(inputAxis, 0).Rotated(Rotation); + + // Calculate the target horizontal velocity. + Vector2 targetHorizontalVelocity = inputDirection * Speed; + + // Move the current horizontal velocity towards the target horizontal velocity by the acceleration factor multiplied by delta. + horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, Accel * (float)delta); + + // Compose the final velocity. + velocity = horizontalVelocity + verticalVelocity; + + Velocity = velocity; + MoveAndSlide(); + } } diff --git a/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs index 30dabd31d9f3..37917823952c 100644 --- a/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs +++ b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs @@ -3,43 +3,50 @@ using _BINDINGS_NAMESPACE_; using System; -public partial class _CLASS_ : _BASE_ +public partial class CharacterBody3d : CharacterBody3D { - public const float Speed = 5.0f; - public const float JumpVelocity = 4.5f; - - public override void _PhysicsProcess(double delta) - { - Vector3 velocity = Velocity; - - // Add the gravity. - if (!IsOnFloor()) - { - velocity += GetGravity() * (float)delta; - } - - // Handle Jump. - if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) - { - velocity.Y = JumpVelocity; - } - - // Get the input direction and handle the movement/deceleration. - // As good practice, you should replace UI actions with custom gameplay actions. - Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); - Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized(); - if (direction != Vector3.Zero) - { - velocity.X = direction.X * Speed; - velocity.Z = direction.Z * Speed; - } - else - { - velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed); - velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed); - } - - Velocity = velocity; - MoveAndSlide(); - } + public const float Speed = 5.0f; + public const float Accel = 5.0f; + public const float JumpVelocity = 4.5f; + + public override void _PhysicsProcess(double delta) + { + Vector3 velocity = Velocity; + + // Add the gravity. + if (!IsOnFloor()) + { + velocity += GetGravity() * (float)delta; + } + + // Get the vertical velocity (for jump). + Vector3 verticalVelocity = velocity.Project(UpDirection); + + // Get the horizontal velocity (for acceleration). + Vector3 horizontalVelocity = velocity - verticalVelocity; + + // Handle Jump. + if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + { + verticalVelocity = UpDirection * JumpVelocity; + } + + // Get the 2D input vector. As good practice, you should replace UI actions with custom gameplay actions. + Vector2 inputVector = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down", 0.15f); + + // Calculate the intended direction in 3D space. + Vector3 inputDirection = Transform.Basis.Orthonormalized() * new Vector3(inputVector.X, 0, inputVector.Y); + + // Calculate the target horizontal velocity. + Vector3 targetHorizontalVelocity = inputDirection * Speed; + + // Move the current horizontal velocity towards the target horizontal velocity by the acceleration factor multiplied by delta. + horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, Accel * (float)delta); + + // Compose the final velocity. + velocity = horizontalVelocity + verticalVelocity; + + Velocity = velocity; + MoveAndSlide(); + } }