Shapecast documentation is misleading!

I’ll keep this simple and quick, I’ve been working on a character controlelr from scratch for the past few weeks, when I replaced my raycast implementation with spherecast for collision detection, there were a bunch of issues, mostly caused by me not knowing that the Distance component of the RaycastResult returned by the sphere cast represents the Distance The Sphere Had To Travel and NOT the Distance Between The Initial Position And The Hit Position.

The reasoning for this assumption was that the documentation states that the spherecast returns a RaycastResult type, and when reading the properties of this type, this is what it says:
Distance: The distance between the ray origin and the intersection point.

Am I missing something here? the only place where I can find the distance by shapecasts correctly mentioned is in the official release post of shapecast.

I was initially a bit confused by this too, and then figured it out by testing. Only later I found it was mentioned in the WorldRoot docs.


(Blockcast and Spherecast)

Distance: The distance between the ray origin and the intersection point is technically accurate because it tells the distance from whatever was the origin.

Shapecast section is missing the details Blockcast and Shapecast section have though, so that is yet to be added. Someone could make a PR (I can do it when I find the time later).

1 Like

I think the reason for this was likely was to ensure consistency across the Raycast API by reusing the RaycastResult type, but they still wanted to provide us with the values to derive both positions.

e.g.

local UserInputService = game:GetService('UserInputService')

local camera = workspace.CurrentCamera
local sphereRadius = 2

local pos = UserInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(pos.X, pos.Y)
local vec = ray.Direction*100

local result = workspace:Spherecast(ray.Origin, sphereRadius, vec, rayParams)
if result then
  -- getting final position of the sphere when intersection occurs
  -- from the `RaycastResult`
  local spherePosition = ray.Origin + ray.Direction*result.Distance

  -- deriving intersection position from the sphere position
  local derivedIntersectionPosition = spherePosition - result.Normal*sphereRadius

  -- getting intersection position from `RaycastResult`
  --
  --   NOTE: we should just use displaced here instead of `direction*magnitude` 
  --         but I wanted to show the `magnitude` component, aka the 
  --         origin-intersect distance 
  --
  local displaced = result.Position - ray.Origin
  local magnitude = displaced.Magnitude
  local direction = displaced.Unit

  local intersectionPosition = ray.Origin + direction*magnitude

  -- working back to the sphere position if we really want to
  local derivedSpherePosition = intersectionPosition + result.Normal*sphereRadius

  -- ... do stuff

end
1 Like

I agree, I also think they might make a new ShapecastResult type for the shapecast api, I think it would make more sense than relying on RaycastResult.

Btw, if you find the documentation misleading, you can submit a pull request to the DevHub documentation repository here.

There’s also an announcement page where you can learn more here but the README on the Github Repo has most of the info you’d need to submit a change.

1 Like