Projectile raycast hitboxes are problematic

I have a local script which generates a projectile that is propelled via linear velocity and detects when it collides with objects with shapecasting. There are two issues I’m currently facing:

  1. The projectile will occasionally ignore the object and pass right through

  2. It doesn’t seem to detect anything on the first shapecast, as shown when I get to close it stops colliding.

Here’s a video of the problems:

Here is the script:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		local mousehit = game.Players.LocalPlayer:GetMouse().Hit
		game.ReplicatedStorage.RemoteEvent:FireServer(mousehit)
		local bullet = Instance.new("Part")
		local force = Instance.new("LinearVelocity")
		local attacment = Instance.new("Attachment")
		attacment.Parent = bullet
		force.Parent = bullet
		force.Attachment0 = attacment
		force.MaxForce = math.huge
		force.VectorVelocity = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.LookVector * 1500
		bullet.Shape = Enum.PartType.Ball
		bullet.Name = "Bullet"
		bullet.Size = Vector3.new(20,20,20)
		bullet.Color = Color3.new(0.556863, 0.333333, 1)
		bullet.Material = Enum.Material.Neon
		bullet.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-4)
		bullet.Parent = game.Workspace
		bullet.CanCollide = false
		bullet.Anchored = false
		bullet.CanQuery = false
		local connection
		connection = game:GetService("RunService").Heartbeat:Connect(function(dt)
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Exclude
			local result = game.Workspace:Shapecast(bullet,force.VectorVelocity * dt)
			local part = Instance.new("Part")
			if not (result == nil) then
				print(result)
				local vfx = game.Workspace.vfx:Clone()
				vfx.Position = bullet.Position + force.VectorVelocity * dt
				vfx.ParticleEmitter:Emit(25)
				vfx.Parent = bullet
				force:Destroy()
				bullet.Anchored = true
				connection:Disconnect()
				bullet.Transparency = 1
				task.spawn(function()
					task.wait(2)
					bullet.CanQuery = false
					bullet.CanCollide = false
					bullet:Destroy()
				end)
			end
		end)
		
	end	
	
end)

Shapecasts don’t detect anything inside of it when they are being casted. It’s likely that when the shapecast is made, it’s being created inside of the model, thus making it ignore the model. You can probably sort of fix this by multiplying the direction of the shapecast by 2 or more. Bug will probably still happen, just less likely. Can’t really do much with what you have right now.

As for the 2nd bug, same reason from above, probably. Shapecasts don’t detect parts already inside of it. You can just fix this by doing a workspace:GetPartsInPart(part,params) and if it hits someone, stop. I guess.

Hell maybe you can just do workspace:GetPartsInPart() every frame to fix both issues. Wouldn’t recommend that though

3 Likes

You probably want to shape cast between the bullet’s last position and it’s current, rather than a forward position predicted by the current velocity. This way if the velocity changes it still shape casts properly (e.g. when the part accelerates at the start because of the linear velocity).

You also aren’t using your params. Probably intentionally.

Interesting! This might be part of the problem:

This probably isn’t that much compute, it’s probably fine. If the problem is what you’re saying, doing this in addition to the shapecast should fix the problem.

Repeatedly doing get parts in part seemed to work alright for the issue of it going through walls randomly, but it still seems to have issues when you fire the projectile up close to another object.

You mentioned shape casting between the bullets last position and the current position. How could I achieve this, assuming it’s applicable to the issue I’m still having.

1 Like

The code would look like this:

local connection
local lastPosition = bullet.Position
connection = game:GetService("RunService").Heartbeat:Connect(function(dt)
	local currentPosition = bullet.Position
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	local result = game.Workspace:Shapecast(bullet, currentPosition - lastPosition)
	local part = Instance.new("Part")
	if not (result == nil) then
		print(result)
		local vfx = game.Workspace.vfx:Clone()
		vfx.Position = currentPosition
		vfx.ParticleEmitter:Emit(25)
		vfx.Parent = bullet
		force:Destroy()
		bullet.Anchored = true
		connection:Disconnect()
		bullet.Transparency = 1
		task.spawn(function()
			task.wait(2)
			bullet.CanQuery = false
			bullet.CanCollide = false
			bullet:Destroy()
		end)
	end
	lastPosition = currentPosition -- Store the position for the next Heartbeat
end)
1 Like

Hello, I tried out this code and it unfortunately didn’t work. I did however manage to instead do a :getpartsinradius for the initial position of about the size of the projectile and it worked. This does still fall apart at even faster speeds but I could most likely just adjust the position and radius.

One issue I do have though that I’ve noticed is that the VFX location is quite inaccurate, especially when transitioning from ray cast hit detection to getpartsinpart/getpartsinradius

You can see in the video once I get close enough to stop ray casting the hitbox starts to go way off depending on the position of the player.