Raycasting inaccuracy

  1. What do I want to achieve?
    Trying to cast a ray in the direction the player’s front view is facing.

  2. What is the issue?
    Raycasting is highly inaccurate. It will raycast to the side sometimes or behind the player.

  3. What solutions have I tried so far?
    Tried doing this:

local raycast = workspace:Raycast(
                plr.Character.UpperTorso.Position,
                plr.Character.UpperTorso.Position + Vector3.new(0, 0, 100),
                raycastParams
        )

but to no avail.

Any help would be greatly appreciated, thanks.

2 Likes

Use CFrame.LookVector

local raycast = workspace:Raycast(
    plr.Character.UpperTorso.Position,
    plr.Character.UpperTorso.CFrame.LookVector * 100,
    raycastParams
)
2 Likes

Hi! In my experience, I find that it’s best to base orientation-based things off the HumanoidRootPart, mainly because it doesn’t rotate with animations (while the UpperTorso does). Try this instead:

local raycast = workspace:Raycast(
                plr.Character:WaitForChild("HumanoidRootPart").Position,
                plr.Character.HumanoidRootPart.CFrame.LookVector*100, --100 studs ahead of where the player is facing
                raycastParams
        )
1 Like

oh wow, the raycast is definitely more accurate now! Thanks

1 Like

mhm, works just fine! although I will have to give the solution to SOTR654 since they posted earlier. thanks for the help though.

1 Like

No problem, glad I could help :happy1:

1 Like