0 votes

I know, there are many tutorials out there, but no of them works at my project.
So I was trying to make an mobile game only with left and right controls.
This are my nodes:

  • Node2D
    -Node2D
    -Touchscreenbutton
    -Touchscreenbutton
    -KinematicBody2D
    -Sprite
    -CollisionPolygon2D
    -Area2D
    -CollisioShape2D
    -CollisioShape2D

My code so far:`extends KinematicBody2D

var velocity = Vector2(250,0)
const SPEED = 550

func _ready():
pass

func getinput():
if Input.is
actionpressed("right"):
velocity.x = SPEED
elif Input.is
action_pressed("left"):
velocity.x = -SPEED
else:
velocity.x = 0

func physicsprocess(delta):
getinput()
move
and_collide(velocity * delta)`

So why does it not work and i tried both moveandcollide and moveandslide, but i dont know why they dont work.

in Engine by (44 points)

Does your kinematicbody2d is your "character" node? How do you know it's not working, what do you execpt ?

1 Answer

0 votes
Best answer

What is not working? collisions or movement?
So, you mapped "left" and "right" actions to some keys? cause i tried on my machine and it works fine. I used this and moved with the arrows:

extends KinematicBody2D

var velocity = Vector2(250,0)
const SPEED = 550

func _ready():
    pass

func getinput():
    if Input.is_action_pressed("ui_right"):
        velocity.x = SPEED
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
    else:
        velocity.x = 0

func _physics_process(delta):
    getinput()
    move_and_collide(velocity * delta)

If you want to change the velocity with the touchscreen buttons, then you should try checking if they are pressed with something like:

if get_node("TouchScreenButtonRight").is_pressed():
    velocity.x = SPEED
elif get_node("TouchScreenButtonLeft").is_pressed():
    velocity.x = -SPEED
else:
    velocity.x = 0

Obviously, replacing for your nodes names in the get_node function.

About the collisions, for KinematicBody2D to collide, those collision shapes have to be children of other bodies, like StaticBody2D, or RigidBody2D. Bodies collide with bodies, they dont collide with just shapes. Areas dont collide, but detect when a body or other area enter or exit the area.

by (3,501 points)
selected by

Sorry, I forgot to tell that the collisions arent working

Collisions with other bodies, right? i see in the tree you showed above, you have some collision shapes and collisions polygons, but for KinematicBody2D to collide, those collisions have to be child of other bodies, like StaticBody2D, or RigidBody2D. Bodies collide with bodies, they dont collide with just shapes. Areas dont collide, but detect when a body or other area enter or exit the area.

Thanks its working now and honestly I feel a little bit stupid, because I looked at a old project and thought that this could't be the problem. Once again thank you for your help

glad to help! You may select the answer so others see its solved. Ill eddit and add about the collisions.

I selected it, didn't knew i could do this

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.