I’m somewhat embarrassed to not know how to do this, but I thought I would ask anyways.
For my FPS weapon system, I’ve never really though about weapon spread until now.
While spread on it’s own is fine and dandy, I’m unsure how I can take the spread amount and convert it into a visual you can see on the crosshair.
Many FPS games will have the crosshair lines space out according to current weapon spread as information to the player regarding where that bullet could land, relative to the screen.
How would I go about creating an accurate dynamic crosshair, where all bullets shot would stay inside the “open area”?
I’m pretty sure you can make 4 frames in a gui and line them up like crosshairs and when the tool is activated you check if its activated again and then change the position using tweenService
Sorry if I wasn’t clear. To clarify, I know Roblox API well enough to set up the animation and look of it, but I’m not sure how to calculate a size/position based on spread.
To calculate the size based on spread you could have the script in the crosshair be something like this in a localscript.
local spread = nil
local lastSpread = nil
local calculateOffset = 1 --Replace this if you want it to be larger or smaller
local rs = game:GetService(“RunService”)
rs.RenderStepped:Connect(function()
spread = gameSpread – put the actual spread here
if spread ~= lastSpread then
rightCrosshairGui:TweenPosition(Udim2.fromOffset(spreadcalculateOffset,0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.2)
leftCrosshairGui:TweenPosition(Udim2.fromOffset(-spreadcalculateOffset,0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.2)
upCrosshairGui:TweenPosition(Udim2.fromOffset(0,-spreadcalculateOffset),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.2) downCrosshairGui:TweenPosition(Udim2.fromOffset(0,spreadcalculateOffset),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.2)
end
lastSpread = spread
end)
I apoligise for the bad code formatting, as I haven’t figured out the devFourm’s code formatting.
Should clear up some more than I know how to change position and such, however I’m unsure how to take a Vector3 spread and convert it into an offset. Thanks for the help tho.
I might be wrong, but wouldn’t using a dynamic crosshair like you describe mean that the crosshair visual gets bigger when shooting at longer distances. When shooting into the sky the potential spread would be infinite (we don’t see fps games expanding the crosshair to the size of the screen).
Maybe I am misundersanding? Can you explain better what you mean by
all bullets shot would stay inside the “open area”?
I figured it out. You can use this code to convert crosshair pixel offset to a Vector3 unit direction
local function getSpreadDirection(input)
local sens = 100
local screenSizeMid = (cam.ViewportSize - Vector2.new(0,36*2))/2
local upOff = (math.random(-input*sens,input*sens)/sens)
local riOff = (math.random(-input*sens,input*sens)/sens)
local ray = cam:ScreenPointToRay(screenSizeMid.X + riOff,screenSizeMid.Y + upOff)
return ray.Direction
end
Yeah i know but that was not what i ment i ment that you could get the vector 2 position and do some simple math by detecting if the player has moved or not without vector3
¨
anyways i am happy that you found out how to make it have a great day