Repository navigation
Create SphereCast3D.json - #2371
Open
Boy1developer wants to merge 1 commit into
Open
Boy1developer wants to merge 1 commit into
Boy1developer wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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) andHitCentroidX/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:
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:
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.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.
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.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
extensions/community/.AI usage