I am trying to make a system to detect when a projectile hits an object by using a raycast from the projectiles old position to the new position every frame, and seeing if the projectile touched anything in transit. Currently, the raycast is triggering in seemingly thin air, but the position of the raycast is multiple studs away from the projectile. Here is the part of my code:
CreateProjectile = function( velocity) – this function creates a visual bullet and handles raycast delay
print(“projectile createdcalled”)
local characters = game.Players
local rayparams = RaycastParams.new()
rayparams.FilterDescendantsInstances = {characters}
local projectile = Instance.new(“Part”)
projectile.CanQuery = false
projectile.Material = Enum.Material.Neon
projectile.Transparency = .5
projectile.Parent = game.Workspace
projectile.Size = Vector3.new(.1,.1,1)
projectile.CFrame = startPoint
projectile.Anchored = true
projectile.CanCollide = false
local projectileStartVector = projectile.Position
local MovementLoop
local hitCheck = false
local projectilePreviousPos
MovementLoop = game:GetService("RunService").Heartbeat:Connect(function()
projectilePreviousPos = projectile.CFrame
projectile.CFrame *= CFrame.new(0, 0, -velocity )
local collisionCheck = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, rayparams)
if collisionCheck and hitCheck == false then
hitCheck = true
print("distance" .. collisionCheck.Distance)
print(projectile.Position)
print(collisionCheck.Position)
print(collisionCheck.Instance.Position)
projectile:Destroy()
MovementLoop:Disconnect()
end
end)
I defined the rayparams:
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player}
local raycastResult = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, raycastParams)
I am then calling the raycast:
local collisionCheck = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, rayparams)
You need to check the result of the Raycast to see if it hit anything.
You can do that by checking if the result is nil:
local raycastResult = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, raycastParams)
if raycastResult then
-- The raycast hit something
end
I am doing that with this code:
if collisionCheck and hitCheck == false then
hitCheck = true
print(“distance” … collisionCheck.Distance)
print(projectile.Position)
print(collisionCheck.Position)
print(collisionCheck.Instance.Position)
projectile:Destroy()
MovementLoop:Disconnect()
end
You defined the variable as raycastResult, but you’re using collisionCheck.
You need to use the correct variable name:
if raycastResult and hitCheck == false then
hitCheck = true
print("distance" .. raycastResult.Distance)
print(projectile.Position)
print(raycastResult.Position)
print(raycastResult.Instance.Position)
projectile:Destroy()
MovementLoop:Disconnect()
end
I am using different variable names but I am still using the same logic:
local collisionCheck = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, raycastParams)
if collisionCheck and hitCheck == false then
hitCheck = true
print("distance" .. collisionCheck.Distance)
print(projectile.Position)
print(collisionCheck.Position)
print(collisionCheck.Instance.Position)
projectile:Destroy()
MovementLoop:Disconnect()
end
You need to check the result of the Raycast to see if it hit anything.
You can do that by checking if the result is nil:
local raycastResult = workspace:Raycast(projectilePreviousPos.Position, projectile.CFrame.Position, raycastParams)
if raycastResult then
-- The raycast hit something
end