Hello Developers, I have a problem with this script, let me explain it first, it fires at a moving block, bullets chase after the block but still miss, why is that? I will show the script.
local ReplicationStorage = game:GetService("ReplicatedStorage")
local Bullet = ReplicationStorage:WaitForChild("Bullet")
local Turret = script.Parent
local fireRate = 0
local bulletDamage = 1
local bulletSpeed = 150
local aggroDistance = 1000
while wait(fireRate) do
local target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local aa = game.Workspace.MovingModel.Platform
if aa then
if (aa.Position - Turret.Position).magnitude < aggroDistance then
local bulletRay = Ray.new(Turret.Position, (aa.Position - Turret.Position).Unit * aggroDistance)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {Turret})
if hit == aa then
target = aa
end
end
end
end
if target then
local aa = target
Turret.CFrame = CFrame.new(Turret.Position, aa.Position)
local newBullet = Bullet:Clone()
newBullet.Position = Turret.Position
newBullet.Parent = game.Workspace
newBullet.Velocity = Turret.CFrame.LookVector * bulletSpeed * 8
newBullet.Touched:Connect(function(objectHit)
local aa = objectHit.Parent:FindFirstChild("Platform")
if aa then
print("hit")
end
end)
end
end
Is the problem that there is a miscalculation or is the velocity speed to slow or…? Any help is appreciated. robloxapp-20220216-1548305.wmv (247.6 KB)
Here is the video, the bullets are missing the block.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local startPos = --Startpos
local endPos = --EndPos
local raycastResult = workspace:Raycast(startPos,endPos,raycastParams)
I got Unable to cast RaycastParams to Ray as an error,
local bulletRay = RaycastParams.new()
bulletRay.FilterType = Enum.RaycastFilterType.Blacklist
local startPos = Turret.Position
local endPos = (aa.Position - Turret.Position).Unit * aggroDistance
local bulletShot = workspace:Raycast(startPos,endPos,bulletRay)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {Turret})
if hit == aa then
target = aa
additionally the reason why its missing if the player is moving is becaue it fires the bullet towards where they are standing at that instance if they move it will continue firing towards that position where they were stood
this should be the desired result though no? are you trying to make homing bullets
also to check if its hit a person the ray (check if the turret can see the player)
RaycastResult.Instance
--then do checks if its a part then if that parts parant is a person then if that person is a player