Topic was automatically imported from the old Question2Answer platform.
Asked By
p7f
Hi:
I have the next scrpit for my player:
extends KinematicBody2D
var speed = 20
var jump_speed = -30
var velocity = Vector2()
var grav = 98
func _ready():
$AnimatedSprite.play()
func _input(event):
if event.is_action_pressed("ui_up") and is_on_floor():
velocity.y = jump_speed
func _process(delta):
velocity.x = 0
if Input.is_action_pressed("ui_right"):
velocity.x += speed
if Input.is_action_pressed("ui_left"):
velocity.x -= speed
if velocity.length() > 0:
if velocity.x < 0:
$AnimatedSprite.flip_v = true
$AnimatedSprite.rotation_degrees = 180
elif velocity.x > 0:
$AnimatedSprite.flip_v = false
$AnimatedSprite.rotation_degrees = 0
if is_on_floor():
if velocity.x == 0:
$AnimatedSprite.animation = "idle"
else:
$AnimatedSprite.animation = "moving"
else:
$AnimatedSprite.animation = "jumping"
if (!is_on_floor()):
velocity.y += grav * delta
velocity = move_and_slide(velocity, Vector2(0,-1))
However, this is not working. For some reason, when velocity.y gets updated to 0 in move_and_slide, is_on_floor no longer returns true, preventing me to jump or change animation. I tryed doing it on _physics_process but it works worse (very slow, and also does not detect is_on_floor).
I read something about a safe_margin, but i cannot figure out how to set that property… any help?