I wish to create a script where I can detect if a part is on the user’s screen. I have looked around on the DevForum and came across a post similar to mine in which a user suggested WorldToViewportPoint and WorldToScreenPoint (another user suggested using :DOT( ) but this has some problems). I tried the provided scripts in the documentation of these two but have had trouble even making slight progress. I can not find any tutorials on these or any posts that tell how to use these. What I wish to do is to detect if a part is on the screen of a player (or multiple players) and if not it will swiftly advance to the closest player but stop in its track once back on the player’s (or another player’s) screen and so on until the part gets close enough to kill the player. Some basic mechanics are a blink meter showing up only when the part comes on screen to which the player would need to pay attention to for if the meter goes down the player blinks causing the part to move closer. Another basic mechanic is the player going into first person and being stuck there at sight of the part but later exiting first person if 30 seconds of not having the part be visible passes. Am I using the correct functions for a type of script like this? If I am, how do i use WorldToViewportPoint and WorldToScreenPoint? What are the differences of the two? What function would be most effective for a script such as this?
I figured it out by myself.
local Part = workspace.Look -- This is the part I want this script to check if is visible
local Camera = workspace.Camera
local Character = game:GetService('Players').LocalPlayer.Character
local RunService = game:GetService('RunService')
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {Character, Part}
Parameters.FilterType = Enum.RaycastFilterType.Blacklist
RunService.RenderStepped:Connect(function()
local Vector, OnScreen = Camera:WorldToViewportPoint(Part.Position)
if OnScreen then
if workspace:Raycast(Camera.CFrame.Position, Part.Position - Camera.CFrame.Position, Parameters) == nil then
warn('Part is Onscreen')
end
end
end)
Put this in a local script in starter player. When the object is in view it will output so.
3 Likes