If the player is at a certain distance from an object, and his head is looking at the object, be visible

Some possible pseudocode:

-Get all parts within view
-Transform their CFrames relative to the player’s camera
-If their position is greater than 0, they’re in front of the camera, if less, they’re behind
-Set transparency

Does the player have to be looking directly at the object or just in the general vicinity?

1 Like

Directly, kpqiaoapqoqoqpqooqoqoapa for 30

I think there is a specific function to get the objects currently being shown in the camera, I forgot the name though, you may want to check the Camera API in the devhub.

If that’s not what you want, you’ll need to use dot product from the object and the characters head.

If you want them to be looking directly at the part, raycasting is probably the best option. You can save a small amount of time if you use the dot product it seems (from my testing) but it’s really not much of a difference (raycasting: 1.5735626220703e-05s, dot product: 8.1062316894531e-06s). These work even faster if you use whitelisting rather than blacklisting.

This is what you’re looking for right:

1 Like

could you please send me this script? I can afford it, Discord : Thiago GGXD#0001

Lol don’t worry about it, this is super simple:
Put this in a LocalScript:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {char}

local dist = 30
local specialParts = {}

char:WaitForChild("Head")

while true do
	local cast = workspace:Raycast(char.Head.Position, char.Head.CFrame.LookVector*dist, params)
	if cast ~= nil and cast.Instance:GetAttribute("IsVisibleWhileLooking") then
		cast.Instance.Transparency = 0
		if table.find(specialParts, cast.Instance) == nil then table.insert(specialParts, cast.Instance) end
		for i, v in ipairs(specialParts) do
			if v ~= cast.Instance then
				v.Transparency = 1
			end
		end
	elseif cast == nil then
		for i, v in ipairs(specialParts) do
			v.Transparency = 1
		end
	end
	wait(0.1) -- I recommend changing this to a heartbeat wait function as wait is inaccurate
end

Then for parts you want it to affect, give the part an attribute called IsVisibleWhileLooking and set it to true so that it looks like this:
csieb_92331

This can handle any number of parts as long as they have that attribute

1 Like