#Gun Raycast bullet visualization
1 messages · Page 1 of 1 (latest)
local function applySpread(direction, spreadAmount)
local spreadRadians = math.rad(spreadAmount)
local spreadX = math.random() * spreadRadians - (spreadRadians / 2)
local spreadY = math.random() * spreadRadians - (spreadRadians / 2)
local spreadZ = math.random() * spreadRadians - (spreadRadians / 2)
return (direction + Vector3.new(spreadX, spreadY, spreadZ)).Unit
end
local function visualizeBullet(muzzlePos, hitPoint, bullet)
local direction = hitPoint - muzzlePos
local length = direction.Magnitude
local middlePoint = muzzlePos + direction * 0.5
local bullet = Instance.new("Part")
bullet.Size = Vector3.new(0.15, 0.15, length)
bullet.CFrame = CFrame.new(middlePoint, muzzlePos + direction)
tweenService:Create(bullet, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Transparency = 1}):Play()
game.Debris:AddItem(bullet, 0.5)
table.insert(excludedObjects, bullet)
end
fireEvent.OnServerEvent:Connect(function(player, muzzlePos, mousePos, spreadAmount)
table.insert(excludedObjects, player.Character)
local character = player.Character or player.CharacterAdded:Wait()
local head = character:FindFirstChild("Head")
local baseDirection = (mousePos - head.Position).Unit * 50
local spreadDirection = applySpread(baseDirection, 70) * 50
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = excludedObjects
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(head.Position, spreadDirection, raycastParams)
local hitPoint
if raycastResult then
hitPoint = raycastResult.Position
else
hitPoint = head.Position + spreadDirection
end
visualizeBullet(muzzlePos, hitPoint)
end)
i have left a few unnecessary parts out
i want it to like like in the game KAT
example below:
I think its because you use a different start position for your ray cast and your visualize function. The part should in theory still expand to your hitpoint. But because your hitpoint is infinitly far away the difference beetwen how the ray actually "travels" and how the visulize part looks like is way bigger.
This is just a theory but i am strongly believing that i am right.
Yeah in this clip the visualize part also starts at the head
probably because of the spread
did you write the code?
or copy from a tutorial?
some parts are mine but the base is from a tutorial
it has nothing to do with the starting position
But think about it. When your part starts somewhere else and your ray starts somewhere else. The visualization of your ray will not be the same
as the rays path
yes this may be right, but ive seen other games with the same gun type where the visualized beam comes from the gun and the actual raycast from the head
because you that the hitpoint is infinitely far away, is it possible to just reduct it to its max range something like that
You can reduce the max range of your part but not the max range of the ray
the ray will travel as long as it doesn't hit something
And the clip you've send litterly shows the ray coming from the players head
yes it was just an example, some other games have its visualized raycast also coming from the muzzle of the gun, but the actual one from the head of the player. just visually more appealing
Well send a clip then
1 second
And how can you proof that the ray actually starts at the head?
if you are for example infront of a wall and aim to a point behind it which is not seeable from first person the visualized bullets endpoint is sort of going where the raycast has it an object
this is the same as in the game ive shown
there's no other way to replicate this
i just want to find out how to calculate to fix this infinitely far away hitpoint as you've mentioned
Well you could check if the length is bigger than your max distance. And then you can just set your parts length to your max distance.
But no that would still use the old direction of your infinitly far away hitpoint nvm
Maybe try creating a part at your max distance before shooting your ray so that your ray finds that part and your hitpoint isn't infinitly far away
Anyways I gotta go now tell me once you've got it or if it there is another problem
yeah ill try