Highlight Players Through Walls With Raycast

Hello! I’m experiencing some troubles by making this script, since I saw that my highlight blinks when the player it’s behind something instead of persist in it properly, can someone help me?

local Players = game:GetService("Players")

local Character = game.Players.LocalPlayer.Character
local Camera = workspace.Camera

local params = RaycastParams.new()
local TestPart = workspace.Ignore
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {TestPart}

local highlight = Instance.new("Highlight")
highlight.Parent = Character
highlight.FillTransparency = 1


game:GetService("RunService").RenderStepped:Connect(function() 
	local cameraPos = Camera.CFrame.Position  
	local characterPos = Character:GetPrimaryPartCFrame().Position 
	local raycastResult = workspace:Raycast(cameraPos, (characterPos - cameraPos), params)
	if raycastResult then 
		highlight.Enabled = true
	else
		highlight.Enabled = false
	end
end)

3 Likes

couldn’t you just use the HighLight property “DepthMode” and set it to “AlwaysOnTop”?

1 Like

That’s not the issue, It’s actually in AlwaysOnTop

1 Like

What I’ve found out, basically when the part has transparency, it works perfectly, but at the moment that it is set to 0, it stops working properly.

Anyway, this stills not solving my problem

1 Like

that’s strange, if you test it in a blank baseplate AlwaysOnTop works just fine even with a Part covering it with Transparency 0.
You could check inside the Character if the HighLight gets deleted by any other scripts ecc… if that’s not the problem then i don’t know how to help you.

It could be because of the direction being incorrect? Try switching it around, take away the camerapos from the characterpos.

i had a 2 issues like this, but i solved one changing the raycast collision group

I don’t think it’s for that, I’ve discovered that as well by removing the CanCollide property from a part as well works fine, the issue starts when it’s a normal part with the CanCollide property on, which I dunno why and doesn’t have too much sense in my point of view.

Do you any examples or something that you can provide to help me with this problem please?

Wait, what about canquery? Have you tried messing around with that?

Nope, didn’t try because I don’t know how to code properly yet hahaha,

I’ve tried what you stated here and seems like to “work” but not at all, like, works but there are some areas that disappears and comes back again and some space areas without any obstacles that activate the highlight, so I don’t know.

Seems to be something regarding my Isometrical Camera system that I have in game, which doesn’t detect at all correctly the character in this case.

I’m kinda frustrated since I’m asking in Discord servers tho and there’s no a solution, although I’m trying everything by myself and with the help of others I’m getting anxious with this and it’s not the final product “at all” since I want to make the highlight better and only show some areas of the character, like this (but the highlight displays in the wall and not outside).

Screenshot_1

But atm I’ll like to fix this and the try the other one

in the raycast parameters, there is a property called “CollisionGroup”, i suggest making a new collision group for the raycast, and make it so it cant collide with the part collision group

	local params = RaycastParams.new()
	params.CollisionGroup = "Raycast"

Actually I have an idea, I had a similiar issue (not related to cameras) but with a raycast gun (I think this is because the direction is so exact). Try multiplying the direction by 2 as a TEST, if that works. Set a custom range using .Unit:

(characterPos - cameraPos) * 2

If it works:

(characterPos - cameraPos).Unit * 200 -- Change the number to fit your custom range

Nope, still not working yet, I don’t know what to do anymore tbh

Tried tho, still not working anyway

You should use this function on the character Root/Torso or whatever, and then if there’s part(s) in the table, put the highlight in them: GetPartsObscuringTarget

Edit: I just read through this more, so while this may not solve your actual issue, it’s probably the easiest way to check if the character is behind parts

Then your game is cursed which is very rare.

1 Like

Also I saw you said something about the isometrical camera. Is it really far away and then you dropped the FOV? That’s what I did in one of my games and it might be causing your issue

I’ll drop here the code in case you can help me because I’m so damn lost now

local Players = game:GetService("Players")

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

local limits = workspace.Ignore.Limits

local params = RaycastParams.new()
params.CollisionGroup = "Raycast"
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {game:GetService("CollectionService"):GetTagged("RaycastCollision")}

--local TestPart = workspace.Ignore
--params.FilterType = Enum.RaycastFilterType.Include
--params.FilterDescendantsInstances = {TestPart}

local highlight = Instance.new("Highlight")
highlight.Parent = Character
highlight.FillTransparency = 1
highlight.DepthMode = 0


game:GetService("RunService").RenderStepped:Connect(function() 
	
	local cameraPos = Camera.CFrame.Position  
	local characterPos = Character:WaitForChild("HumanoidRootPart").Position 
	local raycastResult = workspace:Raycast(cameraPos, (cameraPos), params)
	
	if raycastResult and not raycastResult.Instance:IsDescendantOf(Character) then 
		highlight.Enabled = true
		print(raycastResult)
	elseif raycastResult then 
		highlight.Enabled = false
	else
		highlight.Enabled = false
	end
	
end)

I don’t know, I leave here the code

local zoom = 130
local FOV = 9

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = game.Workspace.CurrentCamera

local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Include
Params.FilterDescendantsInstances = {workspace.Ignore}

Camera.CameraType = Enum.CameraType.Custom

local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
	Camera.FieldOfView = FOV
	if Character then
		if Character:FindFirstChild("Head") then
			game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectCFrame, Character.Head)
			Camera.CFrame =
				CFrame.new(Vector3.new(Character.Head.Position.X + zoom, Character.Head.Position.Y + zoom, Character.Head.Position.z + zoom), Character.Head.Position)

		end
	end
end)
1 Like

Wait did you get this code from a random tutorial? Because you said you weren’t that efficient in coding yet.