#Wall banging with raycast
1 messages · Page 1 of 1 (latest)
you could get the X and Z size and just pick whatever is smallest as a wall will basically always have the length be longer than the width
@tranquil nimbus I would do something similar to this, however this answer isn't sufficient because it has a couple edge cases, most important that if the player shoots at the wall from the side instead of the front, the bullet will still go through even if the wall is 1k studs long because it's using the width and not the length
So you'd need to figure out what size to use based on where the projectile hit the wall
Could you elaborate on this?
I can see how their could be edge cases but I’m confused on the wording of the one you said
Say you always assume that the width of the wall a bullet will travel through is the X size of that wall. This might work perfectly well when you shoot at the wall from the front, however when you try shooting at the side of the wall, it will also go through even if the length of wall the bullet traveled through is way too long because it doesn't account for the fact that you shot at it from the side
Ohhh I get what your saying
--Services
local InputService = game:GetService('UserInputService')
local Players = game:GetService('Players')
--Player
local Player = Players.LocalPlayer
--Objects
local Tool = script.Parent
--Raycasting Parameters
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Player.Character, workspace.Baseplate}
Params.FilterType = Enum.RaycastFilterType.Exclude
--Variables
local MaxWallWidth = 1.5
--[[Code]]
local function Raycast()
--Mouse Position
local MousePos = InputService:GetMouseLocation()
--Raycast positionings
local MouseRayCast = workspace.CurrentCamera:ViewportPointToRay(MousePos.X, MousePos.Y)
local MuzzlePosition = Player.Character.HumanoidRootPart.Position
--Raycast Result
local Raycast = workspace:Raycast(MuzzlePosition, MouseRayCast.Direction * 100, Params)
--Return the raycast
if Raycast == nil then
return nil
else
return Raycast
end
end
local function CheckResultSize(RayCast)
--Get the ray instance
local RayResult = RayCast.Instance
if not RayResult:IsA("Part") then return end
--Get the sizes
local ResultSizeX = RayResult.Size.X
local ResultSizeZ = RayResult.Size.Z
--Check the normal
local Normal = RayCast.Normal
local ResultCFrame = RayResult.CFrame
local PartSize = RayResult.Size
local LocalNormal = ResultCFrame:VectorToObjectSpace(Normal)
if math.abs(LocalNormal.X) > 0.85 then
if ResultSizeX > MaxWallWidth then
print("X too thick")
return
end
elseif math.abs(LocalNormal.Z) > 0.85 then
if ResultSizeZ > MaxWallWidth then
print("Z too thick")
return
end
end
print("Went through!")
end
Tool.Activated:Connect(function()
local Raycast = Raycast()
if Raycast == nil then
print("hit nothing")
return
end
CheckResultSize(Raycast)
end)
I made it all client side cause I was too lazy
here is the Tool Descendants / layout
At first I started using angles but I was having a tough time, I prompted it eventually cause I was getting lazy and it pointed me towards normals and it works well
Ale thanks i got it