Dont really know how to use raycast
Set up some basic raycast thing from a tutorial and im wondering how i could stop the part from going through the wall as shown here
Also anyway to stop this weird delay?
And finally is there a way i can improve the ray so it doesnt look as choppy?
(Code)
local function fire(player, mPos)
if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
shotTime = tick()
if AmmoLeft.Value < 0 then return end
local ShotSound = tool.Barrel.AK47
local fA = script.Parent.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Ak47Fire)
fA:Play()
local DA = script.Parent.Parent:FindFirstChild("Humanoid")
if not DA then return end
if DA.Health <= 0 then return end
local origin = tool.Barrel.Origin.Position
local range = 50
local dir = (mPos - tool.Barrel.Origin.Position).Unit * 50
local Rr = workspace:Raycast(origin, dir, rayP)
if Rr then
print(Rr.Instance.Name)
local sd = Instance.new("Part", workspace.BFolder)
local midpoint = origin + dir/2
sd.Material = Enum.Material.Neon
sd.BrickColor = BrickColor.new("Yellow flip/flop")
sd.Anchored = true
sd.CanCollide = false
sd.CFrame = CFrame.new(midpoint, origin)
sd.Size = Vector3.new(.1,.1, dir.Magnitude)
Debris:AddItem(sd, 0.1)
end
end
I am just going to let you know that there is a module that many people use named FastCast. You do not need to switch over to this module but its there for if you need it:
It is built for making FPS type games. It has bullet drops, bullet velocity, less lag, more efficient. It is used for guns and it inserts parts into the workspace. You can customize these parts for if you have a different gun that shoots out different objects. There are many benefits to this module.
There is a tutorial on YouTube that shows you how to implement it as well:
i was orginally using fastcast for my guns, although i switched to raycast as i wanted my bullets to have a ray like feature
(in another post someone said raycast would be better at making ray type bullets)
If you mean the yellow bullet that comes out of the gun, then you can just use fastcast, but with a beam and two attachments instead.
Beams are also beneficial. They can stop that delay with LockedToPart.
However, it will follow the gun with LockedToPart, you should just use a really quickly destroying beam since most bullets IRL are supersonic and can’t really be seen by the human eye. A great reference would be FE gun kit’s bullets.
The only way for the raycast to clip through the walls is for rayP to somehow whitelist that piece. Could you share what rayP is?
P.S: Using abbreviated variable names makes you code hard to read for people who don’t know where they came from.
local function onEquipped()
local FilterTable = {}
tool.Folder.Grip["AK47 Equip"]:Play()
for i, v in pairs(game.Workspace.Mobs:GetChildren()) do
if v:IsA("Model") and v:FindFirstChildWhichIsA("Humanoid") then
for _, v2 in pairs(v:GetChildren()) do
if v2:IsA("Accessory") then
table.insert(FilterTable, v2)
end
end
end
end
table.insert(CT, tool.Parent)
table.insert(CT, BFolder)
table.insert(CT, game.Workspace.Extras)
table.insert(CT, plrfolder)
print(CT)
rayP.FilterDescendantsInstances = CT
end
We can stop clipping via utilizing the .Distance property of a RaycastResult:
local TS = game:GetService("TweenService")
local function fire(player, mPos)
if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
shotTime = tick()
if AmmoLeft.Value < 0 then return end
local ShotSound = tool.Barrel.AK47
local fA = script.Parent.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Ak47Fire)
fA:Play()
local DA = script.Parent.Parent:FindFirstChild("Humanoid")
if not DA then return end
if DA.Health <= 0 then return end
local origin = tool.Barrel.Origin.Position
local range = 50
local dir = (mPos - tool.Barrel.Origin.Position).Unit
local Rr = workspace:Raycast(origin, range * dir, rayP)
if Rr then
local sd = Instance.new("Part", workspace.BFolder)
local midpoint = origin + Rr.Distance / 2 * dir
sd.Material = Enum.Material.Neon
sd.BrickColor = BrickColor.new("Yellow flip/flop")
sd.Anchored = true
sd.CanCollide = false
sd.CFrame = CFrame.new(midpoint, origin)
sd.Size = Vector3.new(.1,.1, Rr.Distance)
Debris:AddItem(sd, 0.1)
end
end
In addition, my attempt to make it less choppy, we can alternatively do :
if Rr then
local sd = Instance.new("Part")
local midpoint = origin + Rr.Distance / 2 * dir
sd.Material = Enum.Material.Neon
sd.BrickColor = BrickColor.new("Yellow flip/flop")
sd.Anchored = true
sd.CanCollide = false
sd.CFrame = CFrame.new(origin, origin - dir)
sd.Size = Vector3.new(.1,.1, .1)
sd.Transparency = 0
sd.Parent = workspace.BFolder
-- Alternative approach to reduce the "choppiness"
local fadeInTween = TS:Create(sd, TweenInfo.new(.03), {
Transparency = 0,
Size = Vector3.new(.1, .1, Rr.Distance)
CFrame = CFrame.new(midpoint, origin)
})
fadeInTween.Play()
fadeInTween.Completed:Wait()
local fadeOutTween = TS:Create(sd, TweenInfo.new(.07), {
Transparency = 1,
})
fadeOutTween.Play()
fadeOutTween.Completed:Wait()
sd:Destroy()
end
(Code has not been tested and may error / bug, just let me know and i’ll correct it)
I editted a couple more things. You’re not caching anything and your approach to getting the humanoid and everything is very strange, so I fixed those too. Also, I got rid of the fadeIn, and just left the fadeOut. This may look better:
local TS = game:GetService("TweenService")
-- Cache things. This greatly improves performance...
local scriptParentThing = script.Parent
local function fire(player, mPos)
if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
shotTime = tick()
if AmmoLeft.Value <= 0 then return end
local ShotSound = tool.Barrel.AK47
local humanoid = player.Character:FindFirstChild("Humanoid")
if not humanoid then return end
if humanoid.Health <= 0 then return end
local fA = humanoid:WaitForChild("Animator"):LoadAnimation(scriptParentThing.Ak47Fire)
fA:Play()
local origin = tool.Barrel.Origin.Position
local range = 50
local dir = (mPos - tool.Barrel.Origin.Position).Unit
local Rr = workspace:Raycast(origin, range * dir, rayP)
if Rr then
local sd = Instance.new("Part")
local midpoint = origin + Rr.Distance / 2 * dir
sd.Material = Enum.Material.Neon
sd.BrickColor = BrickColor.new("Yellow flip/flop")
sd.Anchored = true
sd.CanCollide = false
sd.Size = Vector3.new(.1, .1, Rr.Distance)
sd.CFrame = CFrame.new(midpoint, origin)
sd.Parent = workspace.BFolder
task.wait(.05)
local fadeOutTween = TS:Create(sd, TweenInfo.new(.05), {
Transparency = 1,
})
fadeOutTween:Play()
fadeOutTween.Completed:Wait()
sd:Destroy()
end
end
local TS = game:GetService("TweenService")
-- Cache things. This greatly improves performance...
local scriptParentThing = script.Parent
local function fire(player, mPos)
if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
shotTime = tick()
if AmmoLeft.Value <= 0 then return end
local ShotSound = tool.Barrel.AK47
local humanoid = player.Character:FindFirstChild("Humanoid")
if not humanoid then return end
if humanoid.Health <= 0 then return end
local fA = humanoid:WaitForChild("Animator"):LoadAnimation(scriptParentThing.Ak47Fire)
fA:Play()
local origin = tool.Barrel.Origin.Position
local range = 50
local dir = (mPos - tool.Barrel.Origin.Position).Unit
local Rr = workspace:Raycast(origin, range * dir, rayP)
if Rr then
local sd = Instance.new("Part")
local midpoint = origin + Rr.Distance / 2 * dir
sd.Material = Enum.Material.Neon
sd.BrickColor = BrickColor.new("Yellow flip/flop")
sd.Anchored = true
sd.CanCollide = false
sd.Size = Vector3.new(.1, .1, Rr.Distance)
sd.CFrame = CFrame.new(midpoint, origin)
sd.Parent = workspace.BFolder
task.wait(.05)
local fadeOutTween = TS:Create(sd, TweenInfo.new(.05), {
Transparency = 1,
CFrame = CFrame.new(origin + Rr.Distance * dir, origin),
Size = Vector3.new(.1, .1, .1)
})
fadeOutTween:Play()
fadeOutTween.Completed:Wait()
sd:Destroy()
end
end
Now the ray part will shrink towards the target on fade out - that should look better.