Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I ignore a ray if another ray has the same target?

Asked by
Ghost40Z 118
3 years ago
Edited 3 years ago

I am trying to make a combat system for my game. I decided on using raycasting to detect targets infront of the player, but I am having a problem where if two rays have the same target, the target will be hit twice.

        if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then return end
        if Blocking.Value == true then return end
        if Stunned.Value == true then return end

        local Animations = script.Animations

        local Hit1 = Humanoid:LoadAnimation(Animations.Kick1)
        local Hit2 = Humanoid:LoadAnimation(Animations.Kick2)

            --Raycasting
        local RayCastParams = RaycastParams.new()
        RayCastParams.FilterDescendantsInstances = {char}
        RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
            --Settings
        local Eyes = char.HumanoidRootPart.CFrame.lookVector
        local RightVector = char.HumanoidRootPart.CFrame.RightVector
        local LeftVector = -char.HumanoidRootPart.CFrame.RightVector
        local Distance = 10
        local Damage = 12.5
            --Settings
        local RayHitBox1 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, Eyes * Distance, RayCastParams)
        local RayHitBox2 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, (Eyes + RightVector)*Distance, RayCastParams)
        local RayHitBox3 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, (Eyes + LeftVector)*Distance, RayCastParams)

        local Rays = {RayHitBox1, RayHitBox2, RayHitBox3}
        local DetectedHumanoid = {}
        for i,v in pairs(Rays) do
            if v.Instance.Parent:FindFirstChild("Humanoid") then
                if v.Instance.Parent:FindFirstChild("Blocking") then
                    DetectedHumanoid[#DetectedHumanoid+1] = v
                end
            end
        end

How could I detect if any of these 3 rays have the same target and then ignore one of them?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hi Ghost40Z

What I would do here is in the for loop, I would check if the instance of the ray is == to the others ray instance;

local Rays = {RayHitBox1, RayHitBox2, RayHitBox3}
local DetectedHumanoid = {}
for i,v in pairs(Rays) do
    if  v.Instance.Parent:FindFirstChild("Humanoid") and Rays[i] ~= v and Rays[i].Instance ~=  v.Instance then
        if v.Instance.Parent:FindFirstChild("Blocking") then
            DetectedHumanoid[#DetectedHumanoid+1] = v
        end
    end
end
Ad

Answer this question