0 votes

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 physicsprocess(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
Godot version 3.3.2
in Engine by (12 points)

1 Answer

0 votes

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:
https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html

by (20,475 points)

Thank you so much

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.