I can't figure out the collisions...

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

I’m currently working on a game using GDScript, but after looking around a bit, I haven’t been able to find a solution to my problem. I’m having trouble getting my code to detect a collision between two objects: The Player, and spikes. The player is a KinematicBody2D, and the spike object is a RigidBody2D. So there are a few questions:

  1. Should the spike be a StaticBody 2D.
  2. How can I make my code detect collisions between two specific objects and react to that collision.

Yes, I am fully aware of the layer system. No, I have absolutely no clue how to use it. Yes I am aware of Area2D. No, I have no clue how to use it. This is actually my first project in Godot so I have absolutely no Idea what I’m doing.

:bust_in_silhouette: Reply From: kidscancode

Since you fully admit you have no idea how they work, you should probably invest your time in learning how to use Area2D, layers, etc. The fact that you know they exist suggests you’ve seen advice that includes them. That advice is there for a reason.

Detecting collisions is a very common thing. There are numerous tutorials and examples available - I suggest starting with the “Step by Step” section of the official docs, which includes a building a small game that uses Area2D to detect contact between the player and the enemies.

There’s info about the different body types and how collisions work here:

The docs also has a section showing how to detect collisions when using KinematicBody2D in different scenarios:

You might also consider looking at the demo-projects, which show a variety of examples.

Since this is your first project, you really have a lot to learn, so following along with an existing project is probably going to be more instructive than trying to build something on your own for your first try.