How do I create collision with walls?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Schwein
:warning: Old Version Published before Godot 3 was released.

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"):
:bust_in_silhouette: Reply From: kidscancode

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.

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?

Schwein | 2017-07-21 17:22

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

Kinematic character (2D) — Godot Engine (latest) documentation in English

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

kidscancode | 2017-07-21 19:07

@kidscancode where are the demo projects found?

confused | 2018-03-12 11:28

The demos are linked on the download page: Download - Godot Engine

You can also access the demo repository here:
GitHub - godotengine/godot-demo-projects: Demonstration and Template Projects

kidscancode | 2018-03-12 15:46