Raycast Is not Accurate

Let’s say I do A Raycast In front of the character head every X seconds, this would work, but what if i get close to a wall and look at it ?, It won’t work, because the raycast doesn’t detect the wall, just the objects behind the wall, why this behavior happens?, Atleast for me, I haven’t seen people talking about this , it’s rare, Maybe I’m missing something?

If someone could Give a solution or a proper explanation if possible would be very very appreciated!

It’s not rare and it usually occurs because the raycast is a bit too infront of the character, set it to start from the HumanoidRootPart’s position and extent outwards, you should also use RaycastParams to filter out the player’s character.

1 Like

Yes, This is good, but, What if i need to do a raycast gun?, and i need to visualize the bullet beam/part/bullet ?, If i raycast it from the rootpart, it would appear from there

This would put us in a dilemma, since if the raycast is casted from the point of origin of the gun Instead, this same behavior that I mentioned above would be caused again

No not really, you could just use:

Origin-Direction.Unit*2

Where origin is the muzzle of your weapon and Direction is the direction you want to shoot.

Alright, This seems reasonable, So i’ll put this as the solution, But if you or someone else has another deep explanation , I’m all ears

I mean, it’s not really quite hard you know?

You just have to do this:

Let’s say you have an attachment on the muzzle of your gun. Your Direction is the Humanoid’s LookVector. You can do:

local Origin = attachment.WorldPosition
local Direction = HumanoidRootPart.CFrame.LookVector
local Range = 100

local Params = RaycastParams.new()
Params.FilterDescendantInstances = {Character}

local Ray = workspace:RayCast(Origin-Direction*2,Direction,Params)
1 Like

So, If i do it this way, then The Visualizing Part wouldn’t pass thru the wall, right?

Yes but I would suggest to start visualizing the part from the muzzle point and not the ray start.