[v1.5] Create Dynamic Crosshairs! (Updated!)

nvm i figured it out
for those who want to know how here is the script:

local UserInputService = game:GetService("UserInputService")
UserInputService.MouseIconEnabled = false

local UI = script.Parent
local DynamicCrosshair = require(game.ReplicatedStorage.Modules:WaitForChild("DynamicCrosshair")).New(UI, 40, 80)

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

DynamicCrosshair:Enable()

local function startAiming()
	DynamicCrosshair.Spreading.MinSpread = 20
	DynamicCrosshair.Spreading.MaxSpread = 40
	DynamicCrosshair:SmoothSet(20, 0.1)
end

local function stopAiming()
	DynamicCrosshair:SmoothSet(40, 0.1)
	DynamicCrosshair.Spreading.MinSpread = 40
	DynamicCrosshair.Spreading.MaxSpread = 80
end

local function shoot()
	DynamicCrosshair:SmoothSet(80, 0.05)
end

Mouse.Button2Down:Connect(startAiming)
Mouse.Button2Up:Connect(stopAiming)
Mouse.Button1Down:Connect(shoot)
2 Likes

raycast seems to make the gun shoot waaay above

	raycastparams.FilterDescendantsInstances = {char}
	local origin, direction = crosshair:Raycast()
	local raycast = workspace:Raycast(origin,direction * 50000,raycastparams)
	if muzzleflashattachment then
		muzzleflashattachment.ParticleEmitter2:Emit(15)
		muzzleflashattachment.ParticleEmitter3:Emit(15)
		muzzleflashattachment.ParticleEmitter4:Emit(15)
	end
	crosshair:Set(crosshair.Spreading.Spread + configuration.Spread.SpreadIncreasePerShot.Value)

Does the raycasting feature for the dynamic crosshair go with the dynamic crosshair? For example if DynamicCrosshair:FollowMouse(false) then will the raycast go with the crosshair in the center or the mouse if its on a different position on the screen. Second question, can you set the DynamicCrosshair:FollowMouse to false for different platforms, I have tried using methods such as if UserInputService.MouseEnabled and UserInputService.KeyboardEnabled then print("z") DynamicCrosshair:FollowMouse(true) else print("s") DynamicCrosshair:FollowMouse(false) end but on mobile it still follows where the user clicks and last but not least, how can you push up the crosshair for different platforms, on different types of devices, like phones the crosshair overlaps with my gun making it confusing where you are shooting. That’s all thanks!

1 Like

Update


:Shove() now just increases spread by IncreasePerSecond

Weird behavior with GUI INSET is finally fixed

Raycast now stays inside the crosshair

Get the module here: https://create.roblox.com/store/asset/10629104729/DynamicCrosshair

TEST OUT NEW DEMO PLACE:

demo.rbxl (87.2 KB)


5 Likes

bros still supporting this module after all this time

respect honestly

Does anyone know how to make the Raycast function work better with 3rd person shooters

Use the crosshair raycast function and get the hitpoint, then cast a separate ray that goes in the direction toward the hitpoint from the barrel of the gun

@Towphy

Im having a weird problem with the :Raycast() function, it seems to be in reverse?
When ever I tap fire the raycast seems to be hitting the max spread and when ever I shoot repeatedly it goes to the smallest, great module by the way its been really helpfull overall!


Ive managed to fix it but I think it may be a bit tacky.
Im just removing the (self.SizeX * 4)

Before

function CrosshairModule:Raycast()
	local X, Y = Functions:RandomPointsInsideCrosshair(self.Spreading.Spread - (self.SizeX * 4))
	local Camera = workspace.CurrentCamera
	local ViewportSize = Camera.ViewportSize
	local mousePos = UserInputService:GetMouseLocation()

	local screenX, screenY

	if self.FollowingMouse then
		screenX = mousePos.X
		screenY = mousePos.Y
	else
		screenX = ViewportSize.X / 2
		screenY = ViewportSize.Y / 2
	end

	local raycastX = screenX + X
	local raycastY = screenY + Y

	local unitRay = Camera:ViewportPointToRay(raycastX, raycastY)
	return unitRay.Origin, unitRay.Direction
end

After

function CrosshairModule:Raycast()
	local X, Y = Functions:RandomPointsInsideCrosshair(self.Spreading.Spread)
	local Camera = workspace.CurrentCamera
	local ViewportSize = Camera.ViewportSize
	local mousePos = UserInputService:GetMouseLocation()

	local screenX, screenY

	if self.FollowingMouse then
		screenX = mousePos.X
		screenY = mousePos.Y
	else
		screenX = ViewportSize.X / 2
		screenY = ViewportSize.Y / 2
	end

	local raycastX = screenX + X
	local raycastY = screenY + Y

	local unitRay = Camera:ViewportPointToRay(raycastX, raycastY)
	return unitRay.Origin, unitRay.Direction
end
1 Like

Weirdly Enough I am also having the same issue, Where its at its max spread when the cross hair is smallest, and Is extremely precise in center when cross hair is at its biggest

Doing What you suggested fixed it! Also other than that, A lifesaver of a module!