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

How can I use WorldToViewportPoint In order to damage the player (?)

Asked by 2 years ago

I want to make a part that damage you when you look at It, so I am deciding to use WorldToViewportPoint In order to make It, however the problem Is that I am new to It, can anyone help me?

1 answer

Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
local LocalPlayer = game:GetService("Players").LocalPlayer
local PartYouWantToLook = workspace.Part
local Debounce = false
local function isPointVisible(Part)
    local camera = workspace.CurrentCamera
    local WorldPoint = Part.Position
    local vector, onScreen = camera:WorldToViewportPoint(WorldPoint)

    if onScreen then
        local origin = camera.CFrame.Position
        local ray = Ray.new(origin, WorldPoint - origin)
        local Params = RaycastParams.new()
        Params.FilterDescendantsInstances = {Part,LocalPlayer.Character}
        local Hit = workspace:Raycast(ray.Origin,ray.Direction,Params)
        if Hit then
            return false
        end
    else
        return false
    end
    return true
end

while task.wait() do
    if isPointVisible(PartYouWantToLook) and not Debounce then
        Debounce = true
        LocalPlayer.Character.Humanoid:TakeDamage(20)
        task.wait(1)
        Debounce = false
    end
end
0
It works! thanks! Also quick question, how can I make It so It damages the player when looking at a part Inside of a model, because apparently, when a player looks at a torso of a NPC, It doesn't damage them imnotaguest1121 362 — 2y
0
see the ignore RaycastParams add your torso or anything you think its blocking it another thing to note is that this is a local script and you must or need to add a remote event so the server actually knows you died Puppynniko 1059 — 2y
Ad

Answer this question