I’m making a 2d mine game and I want that you can’t mine stone through a stone also quick little note I am very new to raycasting maybe I just don’t understand how it works
I’ve made the raycasting system and to test it I changed the Transparency of the hitPart to .5 but for some reason the hitpart is through another part https://gyazo.com/5af16cce7f9bc852617a00efa6066331
here is my code:
Localscript:
local ray = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local IsBehindObject = SelectedPart:InvokeServer(Mouse.Target, ray)
Script:
SelectedPart.OnServerInvoke = function(player, part, ray)
local character = player.Character or nil
if character == nil then return true end
local HumanoidRootPart = character.HumanoidRootPart or character.WaitForChild("HumanoidRootPart")
local rayOrigin = Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, 0)
local length = 1000
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, ray.Direction * length, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
hitPart.Transparency = .5
end
return false
end
SelectedPart.OnServerInvoke = function(player, part) --No more passing ray through
local character = player.Character or nil
if character == nil then return true end
local HRP = character.HumanoidRootPart or character.WaitForChild("HumanoidRootPart")
local newRay = Ray.new(Vector3.new(HRP.Position.X, HRP.Position.Y, HRP.Position.Z), Vector3.new(part.Position.X, part.Position.Y, part.Position.Z))
local length = 1000
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(newRay.Origin, newRay.Direction * length, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart == part then
hitPart.Transparency = .5
end
end
return false
end
you know that you can’t just get the position of two parts you need to get the position of the origin and then the direction to where the ray will cast
Sorry, I’m busy with something else right now, you can fix it by just converting the position of the mouse.target part to objectspace or get the direction by subtracting the Vector3’s of the target part and rootpart