Godot Version
Godot 4.4.1
Question
I tried adding gravity now my character can’t move
That’s a lot of gravity.
Also, where is the movement code? The only thing in your screenshot setting velocity is the gravity code, which is setting (what appears to me to be) a huge vertical velocity.
There ought to be something processing input and setting velocity.x
somewhere…
You can only have one script attached to a Node at any given time, so what you probably did is you replaced your movement script with the gravity script, so the former is not attached to anything now.
The easiest way forward is to combine them into one script.
If you have any issues, post your code here and we can help you solve the issue. Use preformatted text
for code snippets.
ok
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
var direction = 0
var last_direction = 1
var speed = 280
var acceleration = 200
var friction = 150
var turn_acceleration = 1000
func _physics_process(delta: float) -> void:
direction = Input.get_action_strength("Right") - Input.get_action_strength("Left")
previous_direction()
if direction:
if direction * velocity.x < 0:
velocity.x = move_toward(velocity.x, direction * speed, turn_acceleration * delta)
else:
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
else:
velocity.x = move_toward(velocity.x, 0, friction * delta)
move_and_slide()
func previous_direction():
if direction:
last_direction = direction
else:
if velocity.x < 0: # if you press Left go Left, if nothing is pressed stop
last_direction = -1
elif velocity.x > 0: # if you press Right go Right, if nothing is pressed stop
last_direction = 1
gravity script:
extends CharacterBody2D
var fall_speed = 1500
var gravity = 2500
func _physics_process(delta: float) -> void:
_gravity(delta)
func _gravity(delta: float) -> void:
velocity.y = move_toward(velocity.y, fall_speed, gravity * delta)
Please edit your post to use preformatted text for code snippets, otherwise this is very hard to read.
It’s ok I solved the problem
Super, good for you. Maybe you could post your solution here and mark it as a solution to the problem, so that others can find it in case they ever encounter a similar problem?
Not gonna lie the solution was I forgot to attach the movement script to the player I didn’t know pressing attached script. Removeing the previous script and add the new script.
But does your gravity work now? If you attached your movement script, the gravity script got deattached.
I’m gonna combine itto the movement script and see if it works
It not you