How would you see if a player is facing a model?

I don’t understand why people are recommending Raycasting in a thread where the OP specifically requested that solutions do not require Raycasting.

The best solution for your problem (most likely) is Dot product, which can be called on a Vector3 value in a script or command bar. In this case, it will return a value defined by the variable dot, which is used as a metric to determine if a object is facing its target.

local threshhold = 0 --set this number to the 'percent' at which the object is facing the target (-1 for fully facing away, 1 for completely facing the target); values go for all real numbers between -1 and 1

function ObjIsLookingAtTarget(object,target) --make sure all arguments passed in this function have a CFrame value
    local dot = (object.CFrame.LookVector:Dot(target.Position-object.Position).Unit) -- computes the dot product between the object and target
    print(dot>=threshhold) -- is the object facing the target?
    return dot >= threshhold -- will return a true or false value for later usage
end

Hope this helped!

1 Like

So this will tell if the player is facing the model or not?

Yes. If you are using a model, make sure that you are sending the Model.PrimaryPart as an argument, and not the entire model. The PrimaryPart of the model can be set using Model:SetPrimaryPart() or manually in the in-studio explorer.

For best accuracy, make sure the PrimaryPart is near or exactly at the center of the model.

Error: 13:15:29.889 - Players.Kamlkaze_Kid.Backpack.LocalScript:19: attempt to index nil with ‘HumanoidRootPart’

Nothing printed, and there were no errors, also, were do I define the model?

Do this

local RP = Character:WaitForChild("HumanoidRootPart")

and then replace

local Unit = (Character.HumanoidRootPart.Position - Chair.PrimaryPart.Position).Unit

with this:

local Unit = (RP.Position - Chair.PrimaryPart.Position).Unit

This?

local RP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Range = 500

--initializing functions--

local vector3 = Vector3.new 
local dot = vector3().Dot
----------------------

local ModelName = "Chair" 

local Chair = workspace[ModelName]

game:GetService("RunService").RenderStepped:Connect(function()
	
	local ChairLookVector = Chair.PrimaryPart.CFrame.LookVector
	local Unit = (RP.Position - Chair.PrimaryPart.Position).Unit
	
	local dotProduct = dot(ChairLookVector, Unit)

	if dotProduct >= (0.9) or dotProduct <= (-0.9) then
		print("Looking at chair")
	end

end)
1 Like

even though the player is not looking at the chair, it is printing

The dot product method checks if the player is in range of the chair, if you want to make it so the player is directly looking at the chair you should use the raycasting method.

How do you do that though…

I would using the dot product of the two lookVectors for this.

No I meant the raycasting thing @Stredexz was talking about

How do you do what?

The raycast option you suggested earlier

If you scroll up I posted some code for it

I would put your code into a local script, that is in the model correct?

So you’d put in StarterPlayerScripts.

1 Like

Its working, but for some reason its printing ‘workspace’ even though thats not a model

do

if model and model.Name == "Chair" then
print(model.Name)
end

where would I put that code? (30 chars)