How I can make simple Bullet Wall Penetration? I’m making an FPS game right now and I want to make Wall Penetration, but, I literally don’t know anything about it. Can someone give some hints to make this system?
What solutions have you tried so far? I can’t find any solutions on DevForum. If I find something, I can’t even understand how to write it to my FPS system.
So, If someone helps me, It would be really appreciated. Thanks.
Here is an article from Roblox on how you can do it:
SubtractAsync
To demonstrate SubtractAsync(), the following Script uses the Part1 BasePart from the workspace, negates the Part2, Part3, and Part4 BaseParts from it, then re-parents the resulting UnionOperation to the workspace at the original position of Part1.
local mainPart = workspace.Part1
local otherParts = {workspace.Part2, workspace.Part3, workspace.Part4}
-- Perform union operation
local success, newUnion = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = workspace
end
Basically, when the bullet hits the part you clone a new part at the position and then negate that part from the other part.
What about. Im not saying this is the best idea, just the first thought I have.
Would be using 2 raycasts per shoot. One would be the one that collides against the wall, it would read the “material” of the part (materials would be handled by custom properties you decide, like concrete, plastic etc)
Based on what raycast hits, you know how much a bullet can penetrate the wall (allowed distance). When the bullet hits the part, start a new raycast, keeping the original direction that the first raycast had, but now the raycast would be based on the material, to decide how much it can travel inside the material. At the end if the second raycast hit the player, would mean the material was able to be penetrated
Here’s a basic idea on how you could get it to work:
Fire a raycast in the direction the player is aiming. When it hits something, fire another raycast slightly further away from whatever you hit, towards the part you just hit. Use the RaycastResult of both raycasts to find the distance (using something such as Magnitude).
You can subtract this distance from some arbitrary value, so the bullet can’t penetrate every surface. RaycastResult also includes materials, so you could subtract the value further if the bullet penetrates wood instead of steel.
Here’s the function to calculate penetration distance to get you started:
local function CalculatePenetrationDistance(hit: RaycastResult, ray_direction: Vector3)
local basepart: BasePart = hit.Instance
local raycast_params = RaycastParams.new()
raycast_params.FilterType = Enum.RaycastFilterType.Whitelist
raycast_params.FilterDescendantsInstances = {basepart}
local result = workspace:Raycast(hit.Position + (ray_direction * 10000) / 2, -ray_direction * 10000, raycast_params)
if result then
return (hit.Position - result.Position).Magnitude
end
return 0
end
So, I’ve made a script that calculates wall distance. But, how can I make it so that when a bullet touches a certain part for example with a Wood Material, then it “duplicates” or changes its position? And if, for example, a bullet touches Metal, then it simply destroys.
You can re-fire the ray from where the second ray hits the part. Make sure to set the raycast filter to blacklist the part you just hit so you don’t hit it multiple times.