Collisions between objects.

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

I’m fairly new to Godot and game development in general really. The last project I worked on was also my first and it was just a small python game made out of spaghetti code without an engine. So, needless to say, I have no idea what I’m doing. Currently I’m struggling with making my code detect and react to a collision between two objects. The player, a KineticBody2D, and spikes, a StaticBody 2D. I want to make it so the player is moved to an empty layer and falls through the map if they touch the spikes collider, then the scene is regenerated to act as a death mechanic.

Here is my current code for the player:

extends KinematicBody2D

const UP = Vector2(0, -1)
var speed = Vector2()
export(Vector2) var velocity:Vector2=Vector2(500,0)

func _physics_process(delta):
    speed.y += 9.8

var collision_info=move_and_collide(velocity*delta)
if collision_info:
	velocity=velocity.bounce(collision_info.normal)

if is_on_floor():
	speed.y = 9.8
	if Input.is_key_pressed(KEY_W):
		speed.y = -400

if Input.is_key_pressed(KEY_D):
	speed.x = 150
elif Input.is_key_pressed(KEY_A):
	speed.x = -150
else:
	speed.x = 0


move_and_slide(speed, UP)