You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to make a gun shooting system with raycasting and MeshPart bullets since I’ll be using raycasting to compensate the trouble we are having with Touched event and MeshPart bullets to make it look like a real bullet. -
What is the issue? Include screenshots / videos if possible!
Consecutive raycast doesn’t work as intended. The image is down below about what I want and what happens.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried multiple solutions but didn’t work and there was no answer in Hub.
Basically normal raycast works well but I wanted to add some realistic details and wanted to make bullet drop but raycast can’t be curved. So I tried to divide the raycast into little pieces and fire every raycast consecutively in every heartbeat so that I can change the direction and make it like a curve. Firstly, I tried to divide the raycast into little pieces before make it curvy but it didn’t work. Here is my code and my visualized goal (I used laser to test things out before using a bullet.):
local RunService = game:GetService("RunService")
local Character = nil
local Muzzle = nil
local MousePos = nil --basically the target
local previousPos = nil
local shootingbol = false --prevent the heartbeat event to fire without firing gun
local curcamera = nil --the parent of viewmodel, it will be defined later
local origin = nil --raycast origin, will be defined later
local velocity = 20 --nevermind it
game.ReplicatedStorage.Events.Shoot.OnServerEvent:Connect(function(player, muzzle, mousePos, camera)
if player.Character then
Character = player.Character:GetDescendants()
Muzzle = muzzle --the point of gun
MousePos = mousePos --the target
curcamera = camera --container where viewmodel is at
shootingbol = true -- lets the heartbeat fire
end
end)
RunService.Heartbeat:Connect(function(deltaTime)
if shootingbol then
local origin = origin or Muzzle
local destination = MousePos
local direction = (destination - origin) * deltaTime
local midPoint = origin + direction/2
local laser = game:GetService("ServerStorage").Laser:Clone()
laser.CFrame = CFrame.new(midPoint, origin)
laser.Size = Vector3.new(.075, .075, direction.magnitude)
laser.Parent = workspace
table.insert(Character, laser) -- adding the laser directly to the raycastParams didn't work so that's what I do.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {curcamera, Character}
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
print("hit")
shootingbol = false
else
print("didnt hit")
origin = direction
end
end
end)
robloxapp-20230711-1509063.wmv (3.3 MB)
The yellow laser moves only when I alt+tab and come back. I don’t know why.