I dont understand what im doing wrong with my gravity

Hello. I’m pretty new to Godot nearly 1 month of first of using the engine and i have ran in to a problem .
until now i have only made simple games following tutorials making games where you can move in all four directions and so i thought its finally time to make a platformer .
So i started following a tutorial by heartbeast
and thing were going pretty smoothly until i tried adding gravity and things just stopped working
i added gravity like in the tutorial and my charecter woundnt move left or right i have to press the up button more than once for the charecter to jump.
Here is the code please someone help i hhave spent hours trying to fix it and im loosing my mind :

extends CharacterBody2D
var Velocity = Vector2.ZERO
const acc = 150
const fric = 170
var Direct_x 
func _physics_process(delta):
	apply_gravity()
	if Input.is_action_just_pressed('ui_up'):
		Velocity.y -= 150
	Direct_x = Input.get_action_strength('ui_right')-Input.get_action_strength('ui_left')
	if Direct_x == 0 :
		friction()
	else:
		accelaration(Direct_x)
	move_and_collide(Velocity*delta)
func accelaration(amount):
	Velocity.x = move_toward(Velocity.x,acc*amount,10)
func friction():
	Velocity.x = move_toward(Velocity.x,0,25)
func apply_gravity():
	Velocity.y += 4

Btw i used move_and_collide() here cause move_and_slide() dosent take in arguments anymore and i know velocity is now built in and i tried using move_and_slide() but the movement was very wired so i switched over to move and collide()

Also Can anyone tell me how i can use is_on_floor with move_and_collide()?

Well the point of move_and_slide() is that the character can slide/move even when it’s colliding. So you can collide with the floor and still move left and right.

But with move_and_collide() the character stops when it’s colliding with anything, so it’s normal that it can’t move when gravity makes it collide with the floor.

move_and_collide() is meant for objects that should move until they collide with something, like bullets for example. I don’t know if there’s a good way to make it work for character movement.
Imo you should try using move_and_slide() and figure that out if it has an issue.

yeah I tried move_and_slide() but it doesn’t take in arguments anymore and the movement was very wired
But thanks for the exclamation ill try to figure out what i can do to fix that.

you can write a function that casts a 2Dray downward and detects the ground that way

I don’t know how to do that .
But I made move_and_slide() is working for me now .
i just 15 minutes tweaking somethings and now it works perfectly .
But thank you for the suggestion .

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