Dot product help

  1. What do you want to achieve? Keep it simple and clear!

Basically im recreating the scp “Scp-096”. I need him to activate if the player is looking at its face/head.

  1. What is the issue? Include screenshots / videos if possible!

Math isnt my strongest side. So im a bit stuck. I looked at a tutorial on how to use the Dot product to compare vectors. It works and prints im facing the npc as it should. But i ONLY want it to fire whenever a player is facing the npcs face/head from the side or the front.

Example


This is the current code i use to compare the vectors:

                    local Dist = (v.Character.PrimaryPart.Position - Root.Position).Unit
					local Face = Root.CFrame.lookVector
					local Facing = Dist:Dot(Face)

					if Facing > .5 then
						warn("Facing ")
						return v.Character.PrimaryPart
					else
						return nil
					end

Any help is higly appreciated because im kinda on a tight deadline with this Npc :sweat_smile:

I hope this video helps and since its a scp-096 you have to look at the face and then you can use the camera to do that by using .lookVector as a way to see if its look at it so I hope this helps

1 Like

Yes thanks that info is helpful. But i only have one problem. All this is on the server. Is there no other way than checking the cams lookvector?

Have you thought of using raycasting?
Sources:

Hint: Make the RayOrigin the player’s face decal so you can tell where they’re looking

1 Like

you can use a local script that does this and fires a remote script and then check on server with dot product

I managed to fix it. Just had to change the poses from the HumanoidRootPart to the head. I also used added a wall welded it to his back and used some raycasting to detect if it hit the wall or a valid target. If it hit the wall itll just ignore the player

Root = The starting pos which in this case is the Head

Dot code

	if CheckSight(Root.Position,v.Character.Head.Position,v.Character,{Root.Parent}) and not IsPlrBehind(Root,v.Character.Head) then
					
					local Dist = (Root.Position - v.Character.Head.Position).Unit
					local Face = v.Character.Head.CFrame.lookVector
					local Facing = Dist:Dot(Face)

					if Facing > .5 then
						return v.Character.PrimaryPart
					else
						return nil
					end
					
				end

Raycasting code:

local function IsPlrBehind(Start: BasePart, To: BasePart)
	local Params = RaycastParams.new()
	
	local Hit = game.Workspace:Raycast(Start.Position, (To.Position-Start.Position).Unit * 100, Params)
	
	if Hit then
		return Hit.Instance:IsDescendantOf(Start.Parent)
	end
	
end