Raycast off by about 0.5 studs?

I am currently trying to make myself a raycasting system for my new gun system, but it is not working how I want. Every time that I cast the ray it is off by about half a stud from where I want it to cast. I have been trying to figure out the problem, but I can’t find it.

I have a script to visualize each cast.
this part puts the cast exactly where I want it to be, no raycasting involved:

-- arg1: start pos, arg2: end pos
-- the last arg is the color so that it stands out
bulletVisualizer.visualizeCast(workspace.TestShooter.Attachment.WorldPosition, nextTarget, BrickColor.Green())

this puts the bullet where I am trying to get it to go.

-- this part visualizes the other cast
local unitRay = Ray.new(
    workspace.TestShooter.Attachment.WorldPosition, 
    workspace.Targets.Targets["25"].Position
)

local hitPos, result = raycaster.getHitPosition(
    workspace.TestShooter.Attachment.WorldPosition, 
    unitRay, 
    testMaxLength
)
bulletVisualizer.visualizeCast(baseRay.Origin, result.Position)

this however is off by less than half a stud:

-- in a server script
-- arg1: the start Position, arg2: a unit ray for the direction
-- arg3: max length of the ray. is 25 because test brick is 
-- exactly 25 studs away.
local hitPos, result = raycaster.getHitPosition(
    workspace.TestShooter.Attachment.WorldPosition, 
    unitRay,
    testMaxLength
)

-- in raycast.lua
function module.raycast(startPos, unitRay, maxDist )

      -- do the actual raycast
      local result= workspace:Raycast(startPos.Unit, unitRay.Direction * maxDist)

      -- if there is a result, we hit something
      if result then
            return result
      end
      -- no result, nothing hit
      return nil
end

-- get a supposed hit position, even if nothing is hit
function module.getHitPosition(startPos, unitRay, maxDist )
      local result = module.raycast(startPos, unitRay, maxDist)

      -- the ray did in fact hit something
      if result then
            return result.Position, result
      end
      -- the ray did not hit anything, manually do math
      return unitRay.Origin + unitRay.Direction * maxDist, nil
end

Here is the result:
image

I cannot figure out what the cause of the small displacement is? Any help?

Your first argument to Workspace:Raycast() was startPos.Unit - I am pretty sure you don’t want to unitize a position, so I would assume that that is your problem.

I have tried that. When I take the .Unit it ends up as this:
image

image
You’re using unitRay.Origin here. Can you show me where that ray is made? You may have the origin of it not being the same as startPos. It’s likely your code has more than one math bug, so you’d have to fix them one at a time.

Edit: Nvm, I found where you made your ray:

local unitRay = Ray.new(
    workspace.TestShooter.Attachment.WorldPosition, 
    workspace.Targets.Targets["25"].Position
)

You are not setting the Direction correctly. Direction should be a difference vector that is unitized, not something’s position. Try setting the second parameter of the Ray.new call to:

(workspace.Targets.Targets["25"].Position-workspace.TestShooter.Attachment.WorldPosition).Unit
1 Like