Hello everyone. I am trying to create a paint gun that shoots a projectile, and when the projectile hits something, it splats. The issue with my paint gun is that it is not correctly orientated and sometimes, the ball gets stuck inside something and does not go to the correct position.
Video of the paint gun
Expected behaviour
Code
local Paintballs = game.ReplicatedStorage.Paintballs
local char = script.Parent
local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local splashparams = RaycastParams.new()
splashparams.FilterType = Enum.RaycastFilterType.Whitelist
splashparams.FilterDescendantsInstances = {workspace.Map}
splashparams.IgnoreWater = true
local Splashes = {}
local function CreateSplash(painttype, pos, normal)
local paint = Paintballs:FindFirstChild(painttype)
if paint then
local Splash = Instance.new("Part")
--Splash.Shape = Enum.PartType.Cylinder
Splash.Size = Vector3.new(5, 5, 0.5)
--Splash.Orientation = Vector3.new(0,0,90)
Splash.Position = pos
Splash.Anchored = true
Splash.CanCollide = false
Splash.BrickColor = paint.BrickColor
Splash.Name = "Splash"
Splash.Parent = workspace.Paint
if normal then
Splash.CFrame = normal
end
table.insert(Splashes, Splash)
local Emitter = paint.ParticleEmitter:Clone()
Emitter.Enabled = false
Emitter.Parent = Splash
Emitter.EmissionDirection = "Right"
Emitter:Emit(15)
end
end
local function CreatePaint(painttype)
local paint = Paintballs:FindFirstChild(painttype)
if paint then
local Clone = paint:Clone()
Clone.Name = "Paintball"
Clone.Position = cam["Paint Gun"].PaintSpawner.Position
game:GetService("Debris"):AddItem(Clone, 30)
local bodyvel = Instance.new("BodyVelocity")
bodyvel.Velocity = cam.CFrame.LookVector * 100
bodyvel.MaxForce = Vector3.new('inf', 'inf', 'inf')
bodyvel.Parent = Clone
game:GetService("Debris"):AddItem(bodyvel, 0.001)
local connect
connect = Clone.Touched:Connect(function(hit)
if hit:FindFirstAncestor("Map") then
connect:Disconnect()
local params = RaycastParams.new()
params.IgnoreWater = true
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = {hit}
local ray = workspace:Raycast(Clone.Position, (hit.Position - Clone.Position).Unit * 500, params)
local normal = nil
if ray then
print(ray.Normal, ray.Instance)
normal = CFrame.lookAt(Clone.Position, Clone.Position + ray.Normal)
end
CreateSplash(painttype, Clone.Position, normal)
Clone:Destroy()
end
end)
Clone.Parent = workspace.Paint
end
end
spawn(function()
while wait() do
if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
CreatePaint("Blue")
task.wait(0.05)
elseif UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
CreatePaint("Orange")
task.wait(0.05)
end
end
end)
My best guess is that the projectile ends up stuck in a wall. Since rays can’t detect that it is inside the wall, it returns nothing.
Thanks for reading!