Gravity code giving stack trace errors?

Version: 4.3 stable

So this is my first time using godot and this forum, and I’m using this tutorial https://www.youtube.com/watch?v=gnboGSpjHVQ to try and make a game.

I’m at around 28:44 in the video but when I try and run the game it has two “stack frames” which say

0 - res://player.gd:26 - at function: apply_gravity
1 - res://player.gd:11 - at function: _physics_process

Here is my code:

extends CharacterBody2D

@export var speed=400.0
@export var jump_velocity=-650.0
@export var jump_count=2
@export var grav_mult=2.0

var current_jumps=0

func _physics_process(delta: float) -> void:
	apply_gravity(delta)
	handle_jump()

	# 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)

	move_and_slide()

func apply_gravity(delta):
	if not is_on_floor():
		velocity.y +=  get_gravity() * grav_mult * delta

func handle_jump():
	if is_on_floor(): current_jumps=0
	
	if Input.is_action_just_pressed("ui_accept"):
		if is_on_floor() or current_jumps < jump_count:
			velocity.y = jump_velocity
			current_jumps+=1
			

What does the error say?

“Invalid operands ‘float’ and ‘Vector2’ in operator ‘+’.”

Ah right, get_gravity() returns a vector2, but velocity.y is only the y component, a float. remove the .y

func apply_gravity(delta):
	if not is_on_floor():
		velocity +=  get_gravity() * grav_mult * delta

Thanks, that worked.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.