Skip to content

Commit

Permalink
Removed all instances of velocity being defined for CharacterBody2D (
Browse files Browse the repository at this point in the history
…#6862)

* Removed all instances of velocity being defined for `CharacterBody2D`
  • Loading branch information
Emily committed Mar 3, 2023
1 parent d626c1a commit 3585c66
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
4 changes: 2 additions & 2 deletions tutorials/3d/using_transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ Jump:
if Input.is_action_just_pressed("jump"):
velocity.y = JUMP_SPEED

velocity = move_and_slide(velocity)
move_and_slide()

.. code-tab:: csharp

// Keep in mind Y is up-axis
if (Input.IsActionJustPressed("jump"))
velocity.Y = JumpSpeed;

velocity = MoveAndSlide(velocity);
MoveAndSlide();

All common behaviors and logic can be done with just vectors.

Expand Down
18 changes: 3 additions & 15 deletions tutorials/physics/kinematic_character_2d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ little more like a regular game character:
extends CharacterBody2D

const GRAVITY = 200.0
var velocity = Vector2()

func _physics_process(delta):
velocity.y += delta * GRAVITY
Expand All @@ -164,7 +163,6 @@ little more like a regular game character:
public partial class PhysicsScript : CharacterBody2D
{
const float gravity = 200.0f;
Vector2 velocity;

public override void _PhysicsProcess(float delta)
{
Expand All @@ -189,8 +187,6 @@ This adds basic support for walking when pressing left and right:
const GRAVITY = 200.0
const WALK_SPEED = 200

var velocity = Vector2()

func _physics_process(delta):
velocity.y += delta * GRAVITY

Expand All @@ -201,11 +197,8 @@ This adds basic support for walking when pressing left and right:
else:
velocity.x = 0

# We don't need to multiply velocity by delta because "move_and_slide" already takes delta time into account.

# The second parameter of "move_and_slide" is the normal pointing up.
# In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
move_and_slide(velocity, Vector2(0, -1))
# "move_and_slide" already takes delta time into account.
move_and_slide()

.. code-tab:: csharp

Expand All @@ -216,8 +209,6 @@ This adds basic support for walking when pressing left and right:
const float gravity = 200.0f;
const int walkSpeed = 200;

Vector2 velocity;

public override void _PhysicsProcess(float delta)
{
velocity.y += delta * gravity;
Expand All @@ -235,10 +226,7 @@ This adds basic support for walking when pressing left and right:
velocity.x = 0;
}

// We don't need to multiply velocity by delta because "MoveAndSlide" already takes delta time into account.

// The second parameter of "MoveAndSlide" is the normal pointing up.
// In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
// "MoveAndSlide" already takes delta time into account.
MoveAndSlide(velocity, new Vector2(0, -1));
}
}
Expand Down
8 changes: 2 additions & 6 deletions tutorials/physics/physics_introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,6 @@ the ground (including slopes) and jump when standing on the ground:
var jump_speed = -1000
var gravity = 2500

var velocity = Vector2()

func get_input():
velocity.x = 0
var right = Input.is_action_pressed('ui_right')
Expand All @@ -457,7 +455,7 @@ the ground (including slopes) and jump when standing on the ground:
func _physics_process(delta):
velocity.y += gravity * delta
get_input()
velocity = move_and_slide(velocity, Vector2(0, -1))
move_and_slide()

.. code-tab:: csharp

Expand All @@ -469,8 +467,6 @@ the ground (including slopes) and jump when standing on the ground:
private float _jumpSpeed = -1000;
private float _gravity = 2500;

private Vector2 _velocity = new Vector2();

private void GetInput()
{
_velocity.x = 0;
Expand All @@ -491,7 +487,7 @@ the ground (including slopes) and jump when standing on the ground:
{
_velocity.y += _gravity * delta;
GetInput();
_velocity = MoveAndSlide(_velocity, new Vector2(0,-1));
MoveAndSlide();
}
}

Expand Down

0 comments on commit 3585c66

Please sign in to comment.