Hey, I am currently having issues getting my gun system’s cursor working correctly, I am using the Camera’s CFrame.LookVector as the direction to fire. The cursor uses Raycast and Camera:WorldToScreenPoint but is incredibly unreliably and finicky, Is there a better way to achieve this?
I have tried to use the Player’s Mouse however that causes other Issues which are either impossible or Incredibly hard to fix.
Cursor Code:
local RaycastResult = game.Workspace:Raycast(Tool.Handle.FirePart.WorldPosition, game.Workspace.CurrentCamera.CFrame.LookVector*1000, RayCastParams)
local Vector, OnScreen = game.Workspace.CurrentCamera:WorldToScreenPoint(RaycastResult.Position)
Player.PlayerGui:FindFirstChild("MainGui").MainFrame.Crosshair.Position = UDim2.new(0, Vector.X, 0, Vector.Y)
It seems that I have fixed the Issue, I took a look at the Roblox resource weapons and figured out how the shooting direction works. It uses a function to Raycast and have the Camera.CFrame.Position for Origin and the Camera.CFrame.LookVector for the Direction.
--Raycast Function
function PenetrateCast(RayInfo, IgnoreList)
local tries = 0
local hitPart, hitPoint, hitNormal, hitMaterial = nil, RayInfo.Origin + RayInfo.Direction, Vector3.new(0, 1, 0), Enum.Material.Air
while tries < 50 do
tries = tries + 1
hitPart, hitPoint, hitNormal, hitMaterial = workspace:FindPartOnRayWithIgnoreList(RayInfo, IgnoreList, false, true)
if hitPart and (not hitPart.CanCollide and hitPart.Parent:FindFirstChildOfClass("Humanoid")) == nil then
table.insert(IgnoreList, hitPart)
else
break
end
end
return hitPart, hitPoint, hitNormal, hitMaterial
end
--Calling PenetrateCast Function
local RayInfo = Ray.new(game.Workspace.Camera.CFrame.Position, game.Workspace.Camera.CFrame.LookVector * 500)
local _, HitPosition = PenetrateCast(RayInfo, {game.Workspace.Ignore, self.Character})
--Calculate Direction
local Direction = (HitPosition - self.Tool.Handle.FirePart.WorldPosition).Unit