I am making a platformer game and I dont know how to make the player to dont collide with those default walls! Ive already setted the camera follow but now my player is getting stuck on those default walls!
Here´s the player movement code:
extends KinematicBody2D
const GRAVITY = 600
const WALK_SPEED = 200
const JUMP_FORCE = 350
var velocity = Vector2()
var screen_size
func _ready():
screen_size = get_viewport_rect().size
func _physics_process(delta):
velocity.y += delta * GRAVITY
if Input.is_action_pressed("ui_left"):
velocity.x = -WALK_SPEED
elif Input.is_action_pressed("ui_right"):
velocity.x = WALK_SPEED
else:
# velocity.x = 0
# smoothen the stop
velocity.x = lerp(velocity.x, 0, 0.1)
if Input.is_action_pressed("ui_up") and is_on_floor():
velocity.y = -JUMP_FORCE
velocity = move_and_slide(velocity, Vector2.UP)
# prevent player going out of screen
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
Please help! The player just gets blocked as shown in the image! Like I cant advance more from that position idk why. Sorry my english is bad.
Please help, thanks in advance!
My Player gets stuck when he reaches that blue line when the game is running. I just want to desactivate that. Do you know any way?
I think its because the player can only move to the size of the screen, because when I put the game in FullScreen, I can move much further.
Right there in the code is what you describe:
# prevent player going out of screen
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
If you don't want to limit the player to the screen, remove that code.
On a similar note, make sure you have a camera following the player. The simplest way to do it is to put a Camera2D
as child of the player and set its current
property to true
.