the gravity of the game accumulates ... I would like to know how to solve the problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By toxcm 22

extends KinematicBody2D

const gravity=200

var PlayerName=“tobi”

var hp=100

var denage=16.5

var velocity=Vector2()

var speed=100

onready var animatedSprite=$AnimatedSprite

func _ready():
pass

func _process(_delta):

animation()

func _physics_process(delta):

move(delta)

move_and_slide(velocity, Vector2(0, -1))

func move(_delta):

velocity.x=0

if(Input.is_action_pressed("Left")):
	
	velocity.x -=speed
	
if(Input.is_action_pressed("Right")):
	
	velocity.x +=speed
	
if(!is_on_floor()):
	
	velocity.y += gravity

elif(Input.is_action_pressed("up")):
	
	velocity.y = -200

func animation():

if(velocity.x < 0):
	
	animatedSprite.animation="MoveRight"
	
	animatedSprite.flip_h=true
	
if(velocity.x > 0):
	
	animatedSprite.animation="MoveRight"
	
	animatedSprite.flip_h=false 
	
if(velocity.x ==0):
	
	animatedSprite.animation="Idle"
	
if(velocity.y > 0):
	
	animatedSprite.animation = "Fall"
	
if (velocity.y < 0):
	
	animatedSprite.animation = "Jump"

func danage():
pass

func attack():

pass
:bust_in_silhouette: Reply From: kidscancode

First of all, if you use the “Code Sample” button when pasting your code, the formatting will be preserved.

Your velocity is accumulating even when on the ground because you aren’t using the returned value from move_and_slide() - this is what it’s for. Your code should read:

velocity = move_and_slide(velocity, Vector2.UP)

See here for more information:

Thank you so much

toxcm 22 | 2021-06-13 03:20