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
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.
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)
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.