0 votes

Hello, I'm trying to create a collision effect between the player and a wall, but I do not know what to do. Could someone help me?
principal = main (the game world), personagem = character (the player)


extends Area2D

var vel = 200
var tempElapsed = 0

func _ready():

func _process(delta):
    var d = 0
    var e = 0
    var ac = 0
    var ab = 0
    tempElapsed = tempElapsed + delta

    if Input.is_action_pressed("left") and Input.is_action_pressed("up"):
        ac = -2

    if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
        ab = -2

    if Input.is_action_pressed("left") and Input.is_action_pressed("down"):
        ac = 2

    if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
        ab = 2

    if Input.is_action_pressed("left"):
        e = -2
        animation(5, 1)
    elif Input.is_action_pressed("right"):
        d = 2
        animation(1, -1)
    elif Input.is_action_pressed("up"):
        ac = -2
        animation(6, 0)
    elif Input.is_action_pressed("down"):
        ab = 2
        animation(0, 0)

    set_pos(get_pos() + Vector2((1 * vel * delta * (d + e)), (1 * vel * delta * (ac + ab))))

func animation(int1, int2):
    if(tempElapsed > 0.1):
            if get_node("AnimatedSprite").get_frame() != int1:
            elif get_node("AnimatedSprite").get_frame() == int1:
                get_node("AnimatedSprite").set_frame(get_node("AnimatedSprite").get_frame() - int2)
            tempElapsed = 0

func _on_player_area_enter( area ):
    if area.is_in_group("wall"):
in Engine by (12 points)

1 Answer

+3 votes

You're using an Area2D, so you're going to detect contact, but not collision. You seem to be using the area_enter signal, so that should trigger when the Player's area detects another Area2D entering. Is your print statement firing?

If it is, and you do detect the contact, now you have to create a response. You would have to shift the player's position to the appropriate edge of the wall.

In all honesty, this is an overly difficult way to do this. You can get the desired behavior with a minimal amount of code by using KinematicBody2D for your Player, and StaticBody2D for your walls.

by (21,920 points)

So if this is a very difficult way, I think it's best to use KinematicBody2D and StaticBody2D. How can I create the collision using them?

Using KinematicBody2D is very straightforward. There's a good example in the docs:


If you downloaded the demo projects, there's a working example in there too.

@kidscancode where are the demo projects found?

The demos are linked on the download page: https://godotengine.org/download/

You can also access the demo repository here:

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.