0 votes

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.

in Engine by (15 points)

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?

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

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?

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


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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.