Here is the code. It's a bit silly as this is the 1st time coding all by my own. The issue I face with this code is the character gets stuck when it's in a corner
The Code:
extends KinematicBody2D
class_name PuzzlePlayer
var velocity = Vector2() # The player's movement vector.
var move = true # If player is moving it cannot change it's direction
var right = false # players moving direction
var left = false
var up = false
var down = false
var right_anim = false# used to find if the player is standing
var left_anim = false#on right or left side of wall for animations
onready var anim = get_node("AnimationPlayer")
export var speed = 500 # How fast the player will move (pixels/sec).
func _on_WallDetector_body_entered(body):#return everything to default when
right= false #hits a platform
left = false
up =false
down = false
move = true
func _process(delta):
if Input.is_action_pressed("right") and move:#movement
right = true #direction player should move
move = false
anim.play("Right")
right_anim = true
if Input.is_action_pressed("left") and move:
left = true
move = false
anim.play("Left")
left_anim = true
if Input.is_action_pressed("down") and move:
down = true
move = false
anim.play("Down")
if Input.is_action_pressed("up") and move:
up = true
move = false
anim.play("Up")
if move == false: #motion
if right:
velocity.x += 1
if left:
velocity.x -=1
if down:
velocity.y += 1
if up:
velocity.y -= 1
if right == false: #if not moving it velocity = 0
if left == false:
if down == false:
if up == false:
velocity = Vector2()
if velocity.length() > 0:
velocity = velocity.normalized() * speed
print("hey")
move_and_slide(velocity , Vector2.UP)
if move:#used for animation
animation()
func animation():
if is_on_floor():
anim.play("Idle Down")
if is_on_ceiling():
anim.play("Idle Up")
if is_on_wall() and right_anim:
anim.play("Idle Right")
right_anim = false
if is_on_wall() and left_anim:
anim.play("Idle Left")
left_anim = false