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
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