I want To make my player move continuously until it hits a wall

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sahil Shameer

I want to move a character in same direction until it collides with a wall. I am really new to coding and Godot. I have tried various methods but everything has some bug in it.

Hi! It can be done many ways… we would need to knwo what kind of node are you using for your character, if its a 2D or 3D game, if its top down, side scroller, etc. what have you tried so far? if you are really new to godot, have you completed the official Your First Game tutorial?

p7f | 2020-07-28 18:56

I am using a 2D Kinematic body. I am making a platform game, and i have made my first game

Sahil Shameer | 2020-07-29 01:52

If you are using KinematicBody2D, on a side scroller, just using a constant velocity and move_and_slide should be enough. It will stop moving when it collides with something that blocks the way.
Would you share us something of what you’ve tried or what you have so far?

p7f | 2020-07-29 11:39

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

		right_anim = true

	if Input.is_action_pressed("left") and move:
		left = true
		move = false

		left_anim = true

	if Input.is_action_pressed("down") and move:
		down = true
		move = false

	if Input.is_action_pressed("up") and move:
		up = true
		move = false


	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

	move_and_slide(velocity , Vector2.UP)

	if move:#used for 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

Sahil Shameer | 2020-07-29 14:31


Sahil Shameer | 2020-07-29 14:33

What i see from there, is that you are reseting velocity to a zero vector when not pressing any key. So when no action is pressed, your body will stop moving. Maybe you should remove that if you want continuous movement. Lets continue in your other question, so we dont split efforts.

p7f | 2020-07-29 14:47