Godot Version
v4.3.stable.official.77dcf97d8
Question
hello, I am a complete beginner.
I am having trouble with my movement code, for the most part it is fine, however when I try to multiply velocity for gravity by delta it messes with it
the gravity stops working, it only moves the character down a very small amount when moving side to side. It also messes with the velocity of the jumping.
extends CharacterBody2D
var jump_count = 0
var double_jump = 2
const ACCELERATION = 50
const FRICTION = 90
const SPEED = 800
const JUMP_VELOCITY = -1700
var GRAVITY = 110
const WALL_JUMP_PUSH = 180
var WALL_SLIDE_GRAVITY = 130
var is_wall_sliding = false
func _physics_process(delta):
var input_dir: Vector2 = input()
if input_dir != Vector2.ZERO:
accelerate(input_dir)
else:
add_friction()
player_movement()
jump(delta)
wall_slide(delta)
jump_height()
func input() -> Vector2:
var input_dir = Vector2.ZERO
input_dir.x = Input.get_axis("left", "right")
input_dir = input_dir.normalized()
return input_dir
func accelerate(direction):
velocity = velocity.move_toward(SPEED * direction, ACCELERATION)
func add_friction():
velocity = velocity.move_toward(Vector2.ZERO, FRICTION)
func player_movement():
move_and_slide()
func jump(delta):
velocity.y += GRAVITY
if velocity.y > 0 and jump_count == 0:
jump_count = 1
if Input.is_action_just_pressed("jump"):
print(jump_count)
if jump_count < double_jump:
velocity.y = JUMP_VELOCITY
jump_count += 1
if is_on_floor():
jump_count = 0
if is_on_wall() and Input.is_action_pressed("right") and Input.is_action_just_pressed("jump") and not is_on_floor():
velocity.y = JUMP_VELOCITY
velocity.x = -WALL_JUMP_PUSH
if is_on_wall() and Input.is_action_pressed("left") and Input.is_action_just_pressed("jump") and not is_on_floor():
velocity.y = JUMP_VELOCITY
velocity.x = WALL_JUMP_PUSH
func wall_slide(delta):
if is_on_floor():
WALL_SLIDE_GRAVITY = 110
if is_on_wall() and not is_on_floor():
if Input.is_action_pressed("left") or Input.is_action_pressed("right"):
is_wall_sliding = true
else:
is_wall_sliding = false
else:
is_wall_sliding = false
if is_wall_sliding:
velocity.y += (WALL_SLIDE_GRAVITY * delta)
velocity.y = min(velocity.y, WALL_SLIDE_GRAVITY)
if Input.is_action_pressed("down"):
WALL_SLIDE_GRAVITY += 20
print("sliding")
if Input.is_action_just_released("down"):
WALL_SLIDE_GRAVITY = 130
func jump_height():
if Input.is_action_just_released("jump") and velocity.y < 0 and jump_count < 1:
velocity.y = 50