Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | spinefish222 |
he walks forever until he collides with an object how do i fix that
he used to move normally when i used is action released but it made the game lag so i changed it
extends KinematicBody2D
onready var _animated_sprite = $AnimatedSprite
export var speed = 200
export var gravity = 32
export var jump = 600
var direction = Vector2.RIGHT
var motion = Vector2.ZERO
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
motion.x = speed
direction = Vector2.RIGHT
$AnimatedSprite.flip_h = false
_animated_sprite.play("walk")
elif Input.is_action_pressed("ui_left"):
$AnimatedSprite.flip_h= true
_animated_sprite.play("walk")
motion.x = -speed
direction = Vector2.LEFT
if motion.x == 0 && motion.y == 0:
motion.x = lerp(motion.x, 0, 0.25)
if direction==Vector2.RIGHT:
$AnimatedSprite.flip_h=false
_animated_sprite.play("idle")
elif direction==Vector2.LEFT:
$AnimatedSprite.flip_h=true
_animated_sprite.play("idle")
if is_on_floor():
if Input.is_action_pressed("ui_up"):
motion.y = -jump
motion.y += gravity + delta
motion = move_and_slide(motion, Vector2.UP)