Not sure how i would detect if a player is facing the direction of a part
Tell me exactly what you want to happen.
Hm i want it to check if the player is looking towards the part to make sure the player isnt looking a total other direction when trying to attack a part aka a npc
Use raycasting. Shoot a ray that goes in the direction of the HumanoidRootPart’s LookVector. Make sure that the origin of this ray is the same Y Value as the part. You should probably set the RaycastFilterType to Whitelist, and whitelist the part that will be detected.
There isn’t really much context here so…
Use Raycasting like this:
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local HumanoidRootPart = character.HumanoidRootPart
local raycast = workspace:Raycast(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 10, raycastParams)
if raycast and raycast.Instance == MyPart then
print("Facing part")
end
Are you familiar with Raycasting?
Nope never worked with it. I am working on learning raycasting.
Ill give this a try, Thanks for helping!
you can use WorldToScreenPoint from cam function
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local Part = Workspace["Index A Part Here"]
local MaxDistance = 50
RunService.RenderStepped:Connect(function()
local Vector, IsVisible = Workspace.CurrentCamera:WorldToScreenPoint(Part.Position)
if IsVisible and Vector.Z <= MaxDistance then
print("facing a part")
end
end)
This would work however once u zoom your camera out it stops detecting that your facing the part.
yes cuz theres a condition on MaxDistance vars
Yea and i am not necessarily looking for it to detect if the player is facing the part by going off the camera, Id rather it detect it by the player it self facing the part. My fault for not giving enough context lol
However if i dont seem to find what i am looking for i will set your suggestion as my solution as yours is the closest so far.
local Angle = 0.7 -- change to increase (smaller number, min = -1) or decrease (bigger number, max = 1)
local unit = ((workspace.Part.Position - Character.Head.Position) * Vector3.new(1,0,1)).Unit
local HeadLookVec = Character.Head.CFrame.LookVector * Vector3.new(1,0,1)
local IsFacing = HeadLookVec:Dot(unit) > Angle
if IsFacing then
print(IsFacing)
end
Ahh here is what i needed, Thank you very much!
For some reason I can’t get this code to work. Have you adapted it in any way to make it work with your application?
Im very late, but this is what i think is the best way of doing this
local script in startercharacterscripts
This basically uses the method of seeing if its in range then seeing if its on screen, then using raycast to make sure there are no parts in the way.
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local Part = game.Workspace:WaitForChild("your part")
local Camera = Workspace.CurrentCamera
local MaxDistance = 50
RunService.RenderStepped:Connect(function()
local Vector, IsVisible = Camera:WorldToScreenPoint(Part.Position)
if IsVisible and Vector.Z <= MaxDistance then
local cameraPosition = Camera.CFrame.Position
local direction = (Part.Position - cameraPosition).Unit * MaxDistance
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Camera, script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = Workspace:Raycast(cameraPosition, direction, raycastParams)
if raycastResult then
if raycastResult.Instance == Part then
print("facing the part")
else
print("an object is obstructing the view")
end
else
print("no parts in the way")
end
end
end)