Raycast is offset from mouse position

While raycasting to determine the bullet target, the raycast does not go to the mouse position but instead to a position offset from the mouse position. Does anyone know why this is? The solutions I found on the DevForum did not work, some made the offset lesser but none solved the problem entirely. Thank you in advance for any help.

Code:

local fromP = self.model.Effects.Position
	
	local direction = (position - fromP).Unit * self.settings.RANGE
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {self.model, game.Workspace.Lasers, self.player.Character}
	raycastParams.IgnoreWater = false

	local result = workspace:Raycast(fromP, direction, raycastParams)	

	local midPoint = fromP + direction /2

	local laser = Instance.new("Part")
	laser.Parent = game.Workspace.Lasers
	laser.Anchored = true
	laser.CanCollide = false
	laser.Locked = true
	laser.BrickColor = BrickColor.new("Bright yellow")
	laser.Material = Enum.Material.Plastic

	--local random = Vector3.new(math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread))

	laser.CFrame = CFrame.new(midPoint, fromP)-- + random/1000
	laser.Size = Vector3.new(.075, .075, direction.magnitude)
	
	game.Debris:AddItem(laser, self.settings.RATE)
	
	if (result and result.Instance) then
		print(result.Instance.Name)
		local character, humanoid = Global:GetTargetFromRaycast(result.Instance)
		
		if (character and humanoid) and (character ~= self.player.Character) then
			humanoid:TakeDamage(8)
			
			print("TODO: change to player module damage")
		end
	end

Video of the current behavior:
d1f8133eddb10a3274479189fd1db2f4

1 Like

Try raycast from the camera to the mouse position first, then raycast from the gun muzzle to the first raycast end position

Or just do raycasts based on camera position, and just visualize the raycast from the muzzle of the gun to the end point

If fromP the target position is a part make sure it is anchored.

Normally people use mouse.Hit.Position instead.

in roblox the raycast is not from start to end point. the raycast is from start to direction. so its

workspace:Raycast(fromP, mousepos-fromP, raycastParams)

There’s 2 Ways to solve this,

First One is to set the Raycast Origin to the Camera Position

Second One is setting the Raycast Direction to Something like This

local Camera = workspace.CurrentCamera
local Direction = CFrame.new(Camera.CFrame.Position, Camera.CFrame.Position + Mouse.Hit.LookVector*1000).LookVector * Range

For future reference, here is the updated code that works. I used @Korpsii’s suggestion.

local fromP = cameraPos --self.model.Effects.Position
	
	local direction = CFrame.new(cameraPos, cameraPos + mouseLV*1000).LookVector * self.settings.RANGE --(mousePos - fromP).Unit * self.settings.RANGE
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {self.model, game.Workspace.Lasers, self.player.Character}
	raycastParams.IgnoreWater = false

	local result = workspace:Raycast(fromP, direction, raycastParams)	

	local midPoint = fromP + direction /2

	local laser = Instance.new("Part")
	laser.Parent = game.Workspace.Lasers
	laser.Anchored = true
	laser.CanCollide = false
	laser.Locked = true
	laser.BrickColor = BrickColor.new("Bright yellow")
	laser.Material = Enum.Material.Plastic

	--local random = Vector3.new(math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread))

	laser.CFrame = CFrame.new(midPoint, fromP)-- + random/1000
	laser.Size = Vector3.new(.075, .075, direction.magnitude)
	
	game.Debris:AddItem(laser, self.settings.RATE)
	
	if (result and result.Instance) then
		print(result.Instance.Name)
		local character, humanoid = Global:GetTargetFromRaycast(result.Instance)
		
		if (character and humanoid) and (character ~= self.player.Character) then
			humanoid:TakeDamage(8)
			
			print("TODO: change to player module damage")
		end
	end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.