Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | pizzaguyy |
Im a newbie to coding and i need help with my jump. i want to make it more like mario’s jumps but im confused about the code,
extends CharacterBody2D
const SPEED = 300.0
const ACC = 500.0
const FRICTION = 1500.0
const JUMP_VELOCITY = -300.0
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
@onready var animated_sprite_2d = $CollisionShape2D/AnimatedSprite2D
func _physics_process(delta):
apply_gravity(delta)
handle_jump()
var input_axis = Input.get_axis(“left”, “right”)
handle_acceleration(input_axis, delta)
apply_friction(input_axis, delta)
update_animations(input_axis)
move_and_slide()
func apply_gravity(delta):
if not is_on_floor():
velocity.y += gravity * delta
func handle_jump():
if is_on_floor():
if Input.is_action_just_pressed(“jump”):
velocity.y = JUMP_VELOCITY
else:
if Input.is_action_just_released(“jump”) and velocity.y < JUMP_VELOCITY / 2:
velocity.y = JUMP_VELOCITY / 2
func handle_acceleration(input_axis, delta):
if input_axis != 0:
velocity.x = move_toward(velocity.x, SPEED * input_axis, ACC * delta)
func apply_friction(input_axis, delta):
if input_axis == 0:
velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
func update_animations(input_axis):
if input_axis != 0:
animated_sprite_2d.flip_h = (input_axis < 0)
animated_sprite_2d.play(“run”)
else:
animated_sprite_2d.play(“idle”)
if not is_on_floor():
animated_sprite_2d.play("jump")
It is hard to follow given the unformatted code . You should format your code using this guide
godot_dev_ | 2023-05-30 13:19
I believe the applygravity
function should always be taking place (gravity doesn’t stop in real life when you are on the floor, you are continuously accelerated into the ground.
I can’t see anything immediately obvious, but it appears that you support air jumps, which are conditionally dependent on your y velocity. Maybe adding a variable that tracks how many jumps have been performed (resetting upon landing) and a variable to define the max number of double jumps would make your design more clear and fix your jump issue t make it feel more like mario
godot_dev_ | 2023-05-30 13:27