local player = script.Parent
local TS = game:GetService("TweenService")
game:GetService("RunService").RenderStepped:Connect(function()
local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 15)
if hitPart then
print(hitPart.Material)
end
end)
Waittt… I’ve just looked over the Gyazo. This is not the kind of thing I’m looking for. I’m searching for a specific part, not just any part that is infront of me
local player = script.Parent
local TS = game:GetService("TweenService")
game:GetService("RunService").RenderStepped:Connect(function()
local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 15)
if hitPart:IsA("BasePart") and hitPart.Name == "blabla" then -- check name
-- do stuff
end
end)
They are working on implementing canquery for raycasts soon.
Workaround for right now is that you can whitelist the specific parts using RaycastParams:
local player = script.Parent
local TS = game:GetService("TweenService")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {game.Workspace.Query}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
game:GetService("RunService").RenderStepped:Connect(function()
local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 45, raycastParams)
if hitPart then
print(hitPart.Material)
end
end)
You can put every part supposed to be scanned in a folder and do:
raycastParams.FilterDescendantsInstances = {game.Workspace.Folder:GetChildren()}
I would suggest you use Dot product for something like this
Here is a tutorial about it here,
And if you need an example code you can check out this reply on a previous thread!
This code will detect the part anyways even if its behind a wall, if you don’t want that then you will have to raycast from the player to the part to see if is there something on the way
local hrp = game.Workspace.Dummy.HumanoidRootPart -- something idk im writing this quick
local hrp2 = game.Workspace.LookToPart
local vector = hrp.Position - hrp2.Position
local magnitude = vector.Magnitude
local radius = 50 -- change to what you want
local angle = 45 -- threshold for the angle
if magnitude <= radius then
local dot = hrp.Position:Dot(hrp2.Position)
if dot >= 0 and dot <= math.cos(math.rad(angle)) then
print("found it!!")
else
print("didnt find it")
end
end
This is my code.
It is returning “didn’t find it” when the part is right infront of the HRP.
You should use the lookVector instead of hrp.Position in the dot product.
This is because in your current code, it will calculate an angle using a direction vector from the origin to hrp instead of where the player is facing, and thus the wrong angle.
Here’s some other basic code you can manipulate if it helps you understand it better.
local v1 = lookVector
local v2 = head.Position - target.Position
local dp = v1:Dot(v2)
local mags = v1.Magnitude * v2.Magnitude
local cos = dp/mags
local adjCos = math.clamp(cos, -1, 1)
local theta = math.acos(adjCos)
local angle = math.deg(theta)
I found more information below the reply on the thread you could try this one too
local hrp = plr.HumanoidRootPart -- something idk im writing this quick
local hrp2 = plr2.HumanoidRootPart
local vector = hrp.Position - hrp2.Position
local magnitude = vector.Magnitude
local radius = 3 -- change to what you want
local angle = 45 -- threshold for the angle
local dot = hrp.Position:Dot(hrp2.Position)
if dot >= 0 and dot <= 0.707 )
if magnitude <= radius then
-- code that you want to do after you detect
end
end
Just edit it to your version of the code and try it out, sorry for the bad formatting, I am on mobile
No, but you need to raycast again every 0.1 or so seconds so it updates, btw I forgot to tell you but I recommend the script should be a startercharacterscript (or you can do .characteradded)
Checking the dot product would be unnecessary at that point, if it is negative then the angle is greater than 90 degrees which checking the angle itself would prevent anyway.
You would just have to make sure that angle <= 45, which would mean that it is within the view wanted.