What I’m trying to do is make a landmine, but I don’t want to use spheres as those will go through walls. I want to detect a certain radius and I want to see if they are behind a wall or not. I also want the amount of damage to lower the farther they are! i.e 100 dmg up close 5 super far.
I heard there are shapecasts but I don’t want to fire in one direction but more so in a hemisphere.
Another issue I’ve had with spheres those is that they will only detect one player in my script that I’m using for some reason. Is this the best way to actually do it though? Raycasting with a sphere?
Roblox actually added a new feature called Shapecasting, you can do worldroot:Spherecast to make a sphere. Here’s the link: WorldRoot | Roblox Creator Documentation Edit: I just realised you said you don’t want to use shapecast, but you can just put the sphere in the ground matching with the mine and get the distance from the center of the cast
This should do the job, I put some comments to explain what it does.
Hopefully this helps!
Edit: I know this may not fit your specific use case but it should put you on the right track
local RunService = game:GetService("RunService")
local trigger = script.Parent
local landmine = trigger.Parent
local checkAttachment = trigger:WaitForChild("CheckAttachment")
-- Params for the raycast to check ground
local params = RaycastParams.new()
params.RespectCanCollide = false
params.FilterDescendantsInstances = {landmine}
-- Example Settings
local mineSettings = {
radius = 5,
maxDamage = 75,
minDamage = 25
}
-- Checks for a player and calculates dmg
function checkCast (castResult: RaycastResult)
local castInstance = castResult.Instance
local character = castInstance.Parent
if not character then return end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
-- Makes An Explosion Effect
local explosion = Instance.new("Explosion", workspace)
explosion.Position = checkAttachment.WorldPosition
explosion.BlastRadius = 0
-- Calculates damage based on min and max values
local damage = mineSettings.maxDamage - (mineSettings.maxDamage - mineSettings.minDamage) * (castResult.Distance / mineSettings.radius)
humanoid:TakeDamage(damage)
landmine:Destroy()
end
-- Makes the raycast
function makeCast ()
-- Makes a sphere shapecast from the mine's position using an attachment
local castResult = workspace:Spherecast(checkAttachment.WorldPosition - Vector3.new(0, mineSettings.radius, 0), mineSettings.radius, Vector3.new(0, mineSettings.radius, 0), params)
if castResult then
checkCast(castResult)
end
end
-- Checks every heartbeat
RunService.Heartbeat:Connect(function()
makeCast()
end)