I have made gun npcs in my friend’s game, and currently i am struggling with how raycasts work. The issue is that bullets hit from afar but don’t hit targets that are too close.
Here’s a snippet of code that determines the raycast
coroutine.resume(coroutine.create(function()
local rs
rs = game:GetService'RunService'.Stepped:Connect(function()
local ray = module:CastRay(BULLET.Position,BULLET.Position+BULLET.CFrame.LookVector*5,20,ignore) -- raycast
if ray then
module:Damage(ray.Instance.Parent,math.random(damagemin,damagemax),5,true,ray.Normal,ray.Instance,CF(ray.Position),particle) -- damage function
BULLET:Destroy()
rs:Disconnect()
end
end)
end))
16 views from me no reply, i guess people really like to take easy tasks instead of actually helping (welcome “PRO GRAMERS”)
You don’t provide your actual workspace:Raycast code, you merely call some module to raycast. You also do not provide projectile code.
its possible that the projectile itself passes through the humanoid target before you actually trigger raycast for it to detect (hence the .stepped) you may need to first store the initial position/cframe of bullet, then call .Stepped or .stepped:wait(), then call the raycast
sample gun system code from ACS
function CastRay(Bullet, Origin)
if Bullet then
local Bpos = Bullet.Position
local Bpos2 = cam.CFrame.Position
local recast = false
local TotalDistTraveled = 0
local Debounce = false
local raycastResult
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
while Bullet do
Run.Heartbeat:Wait()
if Bullet.Parent ~= nil then
Bpos = Bullet.Position
-- Set an origin and directional vector
raycastResult = workspace:Raycast(Bpos2, (Bpos - Bpos2) * 1, raycastParams)
recast = false
if not raycastResult then
recast = true
CastRay(Bullet, Origin)
break
end
local RunService = game:GetService("RunService")
local part = script.Parent
local lastPosition = part.Position
local origin = part.Position
local distance = (origin - lastPosition).Magnitude
local direction = (origin - lastPosition).Unit * distance
local raycast = workspace:Raycast(origin, direction)
if raycast then
-- stuff
end
I have rewritten script to be using FastCast, i didn’t want to use FastCast at all. But better than nothing, thanks for atleast trying to help rather than read, scrap and go do something much easier like helping a person tweening their camera on an object.