Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CharacterBody script templates. #100662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
extends _BASE_


const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# The @export annotation allows a variable to be shown and modified from the inspector.
@export var speed: float = 300.0
@export var accel: float = 300.0
@export var jump_speed: float = -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.
var vertical_velocity: Vector2 = velocity.project(up_direction)

# Get the horizontal velocity.
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
vertical_velocity = up_direction * jump_speed

# 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)
var input_axis: float = Input.get_axis("ui_left", "ui_right")

# Calculate the intended direction in 2D plane.
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.
horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, accel * delta)

# Compose the final velocity.
velocity = horizontal_velocity + vertical_velocity

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

extends _BASE_


const SPEED = 5.0
const JUMP_VELOCITY = 4.5
# The @export annotation allows a variable to be shown and modified from the inspector.
@export var speed: float = 5.0
@export var accel: float = 5.0
@export var jump_speed: float = 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.
var vertical_velocity: Vector3 = velocity.project(up_direction)

# Get the horizontal velocity.
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
vertical_velocity = up_direction * jump_speed

# 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)
var input_vector: Vector2 = 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.
horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, accel * delta)

# Compose the final velocity.
velocity = horizontal_velocity + vertical_velocity

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,54 @@

public partial class _CLASS_ : _BASE_
{
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();
}
// The [Export] attribute allows a variable to be shown and modified from the inspector.
[Export]
public float speed = 300.0f;

[Export]
public float accel = 300.0f;

[Export]
public float jumpSpeed = -400.0f;

public override void _PhysicsProcess(double delta)
{
Vector2 velocity = Velocity;

// Handle gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Get the vertical velocity.
Vector2 verticalVelocity = velocity.Project(UpDirection);

// Get the horizontal velocity.
Vector2 horizontalVelocity = velocity - verticalVelocity;

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
verticalVelocity = UpDirection * jumpSpeed;
}

// 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 2D plane.
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.
horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, accel * (float)delta);

// Compose the final velocity.
velocity = horizontalVelocity + verticalVelocity;

Velocity = velocity;
MoveAndSlide();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,54 @@

public partial class _CLASS_ : _BASE_
{
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();
}
// The [Export] attribute allows a variable to be shown and modified from the inspector.
[Export]
public float speed = 5.0f;

[Export]
public float accel = 5.0f;

[Export]
public float jumpSpeed= 4.5f;

public override void _PhysicsProcess(double delta)
{
Vector3 velocity = Velocity;

// Add the gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Get the vertical velocity.
Vector3 verticalVelocity = velocity.Project(UpDirection);

// Get the horizontal velocity.
Vector3 horizontalVelocity = velocity - verticalVelocity;

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
verticalVelocity = UpDirection * jumpSpeed;
}

// 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.
horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, accel * (float)delta);

// Compose the final velocity.
velocity = horizontalVelocity + verticalVelocity;

Velocity = velocity;
MoveAndSlide();
}
}