Raycast direction problem

robloxapp-20220120-1725280.wmv (7.5 MB)
^video^
This is my very first post so if this is in the wrong topic or I didn’t explain something too well, I am sorry.

(I’m not very good at scripting and the code may be a bit messy.)

When I am in front of the wall, the raycast always hits the wall instead of where my mouse is, but once I’m on the other side of the wall, everything works as intended.
I am very confused.

I don’t know where to start when fixing this, and I can’t seem to find anything else on the forum that relates to what I am looking for. If you can find a post with the same problem I have, a link would be much appreciated!


function newbullet()
	local range = -1000
	local origin = Iteminhand.Muzzle
	local bulletparams = RaycastParams.new()
	bulletparams.FilterDescendantsInstances = {VM, char}
	bulletparams.FilterType = Enum.RaycastFilterType.Blacklist
	local pos = (origin.Position - mouse.Hit.p).unit * range
	local bullet = workspace:Raycast(origin.Position, pos, bulletparams)
	if bullet then
		local hit = bullet.Instance
	         if hit then
			local xr = math.random(1,255)
			local yr = math.random(1,255)
			local zr = math.random(1,255)
			hit.Color = Color3.new(xr, yr, zr)
			print("Hit!")
		end
		print(bullet)
	end
end
	
	
1 Like

Your direction is wrong.

Should instead be:

(mouse.Hit.p - origin.Position).Unit

Tried that, for some reason now it’s the same problem except instead it’s hitting the back wall of the building instead of the wall with the green text on it.

Pretty sure this wont fix it. But I recommend you do mouse.hit.Position. Since hit.p is deprecated,

That won’t fix it (still just the position property at the end of the day).

local range = 1000
local pos = (mouse.Hit.Position - origin.Position).Unit * range
local bullet = workspace:Raycast(origin.Position, pos, bulletparams)

Negative range was your issue.

I said above that it wouldn’t fix. I was just recommending him to use another thing instead of a deprecated one

This is fixed with the negative range, though yeah: that should probably be switch and range should be 1000.


The only possible problem that I can see is that the origin is wrong. Are you sure it’s shooting from the right spot?

It would make sense that if you’re shooting from the wrong point it would sometimes hit the intended spot and sometimes random things would get in the way.


I believe this is canceled out by having direction (aka pos) be (initial position - final position) instead of (final position - initial position).

The origin is a part welded to the end of the barrel. Should I use a different spot for the origin?

For some reason I had the operands the other way round, I must’ve copied the line from the first reply.

2 Likes

I wrote some code to visualize raycasts:

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local folder = Instance.new("Folder")
folder.Name = "RaycastVisualizerFolder"
folder.Parent = ServerStorage


local fade = true
local transparency = 0.1
local duration = 3
local color = Color3.new(1, 0.075, 0.075)
local width = 0.1

local function createIndicator(origin, direction)
	local newIndicator = Instance.new("ConeHandleAdornment")
	newIndicator.Color3 = color
	newIndicator.Radius = width/2
	newIndicator.Height = direction.Magnitude
	
	-- Distance cap
	if newIndicator.Height > 1000 then
		newIndicator.Height = 1000
	end
	
	newIndicator.Transparency = transparency
	
	local newPart = Instance.new("Part")
	newPart.CFrame = CFrame.lookAt(origin, origin + direction)
	
	
	
	newPart.Parent = folder
	newIndicator.Adornee = newPart
	newIndicator.Parent = Workspace
	
	
	if fade then
		local tween = TweenService:Create(newIndicator, TweenInfo.new(duration, Enum.EasingStyle.Linear), {Transparency = 1})
		tween:Play()
		local connection
		connection = tween.Completed:Connect(function()
			tween:Destroy()
			connection:Disconnect()
		end)
	end
	
	Debris:AddItem(newIndicator, duration)
	Debris:AddItem(newPart, duration)
end

local RaycastVisualizer = {}

RaycastVisualizer.Raycast = function(...)
	createIndicator(...)
	return Workspace:Raycast(...)
end

RaycastVisualizer.SetFadeEnabled = function(newFade)
	fade = newFade
end

RaycastVisualizer.SetTransparency = function(newTransparency)
	transparency = newTransparency
end

RaycastVisualizer.SetWidth = function(newWidth)
	width = newWidth
end

RaycastVisualizer.SetColor = function(newColor)
	color = newColor
end

return RaycastVisualizer

You can use it like this:

local moduleScript -- Set to ModuleScript with code
local raycastVisualizer = require(moduleScript)

local result = raycastVisualizer.Raycast(origin, direction, params)

It does stuff like this:

ezgif.com-gif-maker (21)
(It uses ConeHandleAdornments so it doesn’t add more raycasting targets like parts)

I was wondering if origin is set correctly. If it works properly only from some locations it sounds like a problem with the origin.

I guess maybe just double check that Iteminhand is the correct instance (not a similar one in a storage location) and that Iteminhand.Muzzle is positioned properly.

2 Likes

I was accidentally using the model that was in ReplicatedStorage instead of the one that was in the viewmodel and now that I have it set to the one in the viewmodel, it just shoots at the baseplate now. If it is a problem with the origin where should I put it? it’s welded to the barrel, and I checked that the weld wasn’t broken or something while I was testing. Sorry if I misunderstood something or if im doing something wrong, I’m not the best at this stuff :sweat_smile:

1 Like

function newbullet()
local range = 1000

    local origin = workspace.Camera.VM:FindFirstChild(Iteminvm).Muzzle

local moduleScript = game.ReplicatedStorage.thingmod

local raycastVisualizer = require(moduleScript)

local bulletparams = RaycastParams.new()

local pos =  (mouse.Hit.Position - origin.Position).unit * range

local bullet = workspace:Raycast(origin.Position, pos, bulletparams)

local result = raycastVisualizer.Raycast(origin.Position, pos, bulletparams)
origin.Transparency = 0
print(origin.Position)

bulletparams.FilterDescendantsInstances = {VM, char}
bulletparams.FilterType = Enum.RaycastFilterType.Blacklist




if bullet then

	local hit = bullet.Instance
	if hit then
		local xr = math.random(1,255)
		local yr = math.random(1,255)
		local zr = math.random(1,255)
		hit.Color = Color3.new(xr, yr, zr)
		print("Hit!")
	end
	print(bullet)
end

end

I am confused and I don’t know what the problem was, but I found when I was messing around and made it update the origin every time you shot before the raycast fired and it works now. Thanks for the help

1 Like