Velocity Speed, Raycasting

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.

It could be server delay also Ray.new() is deprecated. Use RayCastParams() instead.

local bulletRay = RaycastParams.new(Turret.Position, (aa.Position - Turret.Position).Unit * aggroDistance)

Did this and got an error, Unable to cast RaycastParams to Ray. How should I use RaycastParams()?

Here’s an example.

local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local startPos = --Startpos
	local endPos = --EndPos
	
	local raycastResult = workspace:Raycast(startPos,endPos,raycastParams)

what is

local startPos = --Startpos
local endPos = --EndPos

My bad I forgot to include:

local startPos = Turret.Position
local endPos = (aa.Position - Turret.Position).Unit * aggroDistance
1 Like

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

For hit detection use

if raycastResult then
   print("Hit")
end

Also to ignore certain parts use:

	raycastParams.FilterDescendantsInstances = {turrent}

It prints Hit, what else should I do?

now the problem is

local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {Turret})
				if hit == aa then
					target = aa

still gives the same error

I think you should remove this and do what you’re tying to do which is

if raycastResult then
   target = aa
   print("Hit")
end
					
1 Like

Now that you updated your ray you should hit the player. So maybe you should try tweening the projectile so it’s certain it will hit.

How should I do that? The script works again btw thanks

You can tween an object by using tween service here is an example:

local TweenService = game:GetService("TweenService")
local TweenInfo = TweenInfo.new(0.25,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)
local Goal = {Position = endPos}
local Tween = TweenService:Create(Bullet,TweenInfo,Goal)
Tween:Play()
Tween.Completed:Wait()
Bulllet:Destroy()

because im pretty sure u are meant to do

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {caster.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

assign the raycast params as shown and then plug it into a raycast and the raycast result is shown here VVVV
https://developer.roblox.com/en-us/api-reference/datatype/RaycastResult

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
1 Like