Raycast vs CollisionShape2D

Godot 4.3 Stable

I want to make a platformer that can walk up slopes and stuff and get the slope angles. I need 6 collision checks, 2 for the top for ceiling collision, 2 on the bottom for floor collision, and 2 in the middle for push checks. Now how should i do it, would a raycast or collisionshape2d work more better?

It is the work of raycast not collision shape. So you can use it.

  1. Collision shape is used to collide with other collisions.
  2. Raycast is used to detect any collision shape or body.

CollisionShape2D in itself does not do anything. It only provides a shape for a parent node to use. If you were to use a StaticBody2D with a CollisionShape2D child, your StaticBody2D will collide. Area2Ds also use CollisionShape2Ds for their object detection.

If you want, you could use:

  1. Area2D
    Area2Ds detect collisions with other objects, but they can also overlap objects, reporting more than 1 collision. Requires a CollisionShape2D or CollisionPolygon2D child. Only updates at the end of every frame for computational reasons.
  2. Raycast2D
    A single line cast where ever you want that starts at the base and moves toward the target, stopping if it hits something. It will report at most 1 collision, but could report none.
  3. ShapeCast
    Like a Raycast2D, except with any shape; Could be a circle, square, rectangle, triangle, whatever. More computationally expensive than a Raycast.

I personally would use a Raycast2D, but if you stand slightly over the edge of a cliff with the Raycast2D centered, you may not detect any collisions. Also, if you are using a CharacterBody2D, they have built-in methods for detecting walls, floors, and ceilings. See in_on_floor().

Hope this helps!

Using Raycast2D is ideal for detecting collisions with slopes and getting angles. CollisionShape2D defines physical boundaries, but Raycast2D helps detect collisions precisely along lines, which suits your need for multiple checks.