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

How would I go about changing a model's collision if it's being looked at?

Asked by
zyklp 7
6 years ago

Let's say I want a model to lack collision if it's off-screen, but I want cancollide to be on if it's on-screen, how would I accomplish this?

1 answer

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

First, you would use WorldToScreenPoint to tell if a part's center was on the screen:

local function IsOnScreen(Part)
    local Point, Bool = Camera:WorldToScreenPoint(Part.Position)
    return Bool
end

However, this will only tell you if the part is on the screen. To tell if it is not hidden behind an object, you can use raycasting from the camera to the part to detect any other parts that may be in the way:

local function IsVisible(Part)
    local R = Ray.new(Camera.CFrame.p, (Part.Position - Camera.CFrame.p).unit * 999)
    local DetectedPart = workspace:FindPartOnRay(R,Part)
    return not not DetectedPart
end

This example would return true if there was a part in the way of the camera and the part, by casting a ray from the camera to the part.

You would then regularly iterate through the parts you want to check and can apply these functions as outlines to detect if they are visible.

0
Really helpful answer! TiredMelon 405 — 6y
Ad

Answer this question