Skip to content

Create SphereCast3D.json - #2371

Open
Boy1developer wants to merge 1 commit into
GDevelopApp:mainfrom
Boy1developer:main
Open

Boy1developer wants to merge 1 commit into
GDevelopApp:mainfrom
Boy1developer:main

Conversation

@Boy1developer

@Boy1developer Boy1developer commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Cast a sphere through the 3D scene or check which objects a sphere touches, with a preview debug drawing.

How to use it

All the actions, conditions and expressions are in the "Sphere Cast 3D" group. A cast (or an overlap) is saved under a name you choose, and everything that reads the result takes that same name. That also means you can run several casts side by side. Positions, radius and distances are in pixels.

Sphere cast

Add "Sphere cast (from angles)" to sweep a sphere from a point, with a yaw and a pitch in degrees. If you prefer a direction vector, use "Sphere cast" instead. In both cases you give the objects (or object group) to test against, a name, the origin, the radius and the max distance.

This is what the example project does every frame to aim from the player:

Sphere cast "Aim" against SphereCastTargets: origin (Player.CenterX(); Player.CenterY(); Player.Z() + Player.Depth() / 2), yaw Player.Angle(), pitch CastPitch, radius CastRadius, max distance CastMaxDistance, bounding box only: UseBoundingBox

Then check the "Sphere cast hit" condition with the same name. When it is true you can read HitObjectName, HitDistance, HitPointX/Y/Z (contact point), HitNormalX/Y/Z (surface normal) and HitCentroidX/Y/Z (center of the sphere when it touched). Only the closest object is reported.

To act on the object that was hit, use "Pick the object hit by the sphere cast". In the example, pressing E on a coin you are aiming at is just:

Key E pressed
Pick Coin hit by sphere cast "Aim"
    -> Delete Coin

A collider that already overlaps the sphere at its origin is ignored by the cast, so if the cast seems to go through a wall, the radius is probably bigger than your distance to it. "Sphere overlap" is the tool for that case.

Sphere overlap

"Sphere overlap" checks what a sphere touches at a fixed position, without moving it. It counts an object when the sphere crosses it, rests against it or is completely inside it. The example keeps a probe sphere 90 px in front of the player:

Sphere overlap "Probe" with SphereCastTargets: center (point 90 px ahead of the player), radius CastRadius, bounding box only: UseBoundingBox

Use "Sphere overlap touches something" to react, OverlapCount("Probe") to know how many objects are touched, and "Pick the objects touched by the sphere overlap" to keep only those objects.

  • Debug drawing *

Call "Debug draw sphere cast" or "Debug draw sphere overlap" right after the action to see it in the preview. With a duration of 0 the drawing lasts one frame, so call it every frame (that is what the example does). For a cast, cyan is the start sphere, green is the end sphere (red when something is hit), yellow is the contact point, blue is the surface normal and magenta is the triangle that was hit. For an overlap, the sphere is green when it touches nothing and red when it touches something. Nothing is drawn in an exported game, and "Clear sphere cast debug drawings" removes whatever is still on screen.

  • Exact mesh or bounding box *

By default the objects are tested against their real 3D mesh. Set the last parameter ("bounding box only") to 1 to use the bounding box instead: it is faster but less precise, and near misses start to count as hits. CastInfo("Aim") tells you how many objects were tested with their mesh or with their box, which helps when a result looks wrong.

  • Example project *

My project9.zip

A small 3D scene where the player walks around and sweeps a sphere forward every frame. A panel on the left shows everything the extension returns for the cast and for the probe overlap. Controls: W A S D and Space to move, Up / Down for the sphere radius, Left / Right for the max distance, R / F to aim up or down, B to switch between exact mesh and bounding box, G for the debug drawing, E to collect the coin you are aiming at. The project comments suggest a few things to try (radius 0 to get a plain ray, a coin hidden behind a block, walking into a wall with the probe).

Checklist

  • I've followed all of the best practices. The checks of this pull request must pass.
  • New extensions are added to extensions/community/.
  • The example project is the whole folder of my project, not a build of my game.
  • The example project is attached as a ZIP file, not linked to an external service like Google Drive.
  • I've considered adding my extension to the GDevelop community list, but think it's better to have it here.
  • I confirm that this extension can be integrated to this GitHub repository, distributed and MIT licensed.
  • I am aware that the extension may be updated by anyone, and do not need my explicit consent to do so.

AI usage

  • I used an (cluade)to generate some part of the code.
  • I don't understand the generated code or I don't feel able to modify it.

@Boy1developer
Boy1developer requested a review from a team as a code owner November 23, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant