-
What do you want to achieve? Keep it simple and clear!
I want to cast a ray from the head of an enemy to the head of a player.
-
What is the issue? Include screenshots / videos if possible!
While the raycast itself works properly, A ray is only fired directly ahead of the enemy’s head.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried changing multiple values on line 9, without any success.
The following block of code is the ray firing directly ahead, as well as the closest I’ve gotten to getting this to work as intended.
local db = false
game.ReplicatedStorage.FoundPlayer.OnServerEvent:Connect(function(plr)
local params = RaycastParams.new()
local Viewpoint = script.Parent.Head.ViewpointAttachment
local PLRAttach = Instance.new("Attachment")
PLRAttach.Name = "TargetAttachment"
PLRAttach.Parent = plr.Character.Head
PLRAttach.Position = plr.Character.Head.Position
local ray = game.Workspace:Raycast(script.Parent.Head.Position, Viewpoint.CFrame.LookVector*5000, params)
if ray then
PLRAttach.Position = ray.Position - script.Parent.Head.Position
if ray.Instance.Parent:FindFirstChild("Humanoid") and not db then
db = true
game.Workspace.Found:Play()
wait(5)
db = false
end
end
PLRAttach:Destroy()
end)
Try changing PLRAttach.Position = plr.Character.Head.Position
to PLRAttach.CFrame = plr.Character.Head.CFrame
I just tested, and it functions just the same as if I was using .position
Alright, NOW I see your problem, I just had to scroll right in the code.
So on local ray = game.Workspace:Raycast(script.Parent.Head.Position, Viewpoint.CFrame.LookVector*5000, params)
you didn’t add the origin to the LookVector. Replace with local ray = game.Workspace:Raycast(script.Parent.Head.Position, script.Parent.Head.Position+Viewpoint.CFrame.LookVector*5000, params)
which should make it so that the orientation is correct. There might still be some problems after this in the future, so make your code final before marking this as the solution (if it is).
I did replace the line as you stated, but it continues firing the ray in a straight line. I’m not sure if there is something I’m doing wrong or if there is a line of code that I need which I’m missing.
Maybe give some images describing the problem?
I can’t exactly provide images that effectively describe the issue, but I can state what’s intended;
I’m trying to have an AI notice a player when they’re nearby and in line of sight but can’t see through walls. I just need to have the ray cover the angles in which the AI can see
This image is a the script which checks if the player is within line of sight, fully functional.
This image is the script I’m trying to fix.
Did you configure the parameters to only blacklist certain objects?
No, I haven’t. There isn’t a line which allows the ray to ignore a set of objects
In the RaycastParams there’s FilterType and set it to Enum.RaycastFilterType.Blacklist
Just tested, and the Ray still seems to only fire straight.
Is ray returning nil? Try removing - script.Parent.head.Position
and see what happens. Is there an error in the output?
There is no error in the output, and the ray continues to fire only straight-foward.
What is the value of ray? Is ray.Instance nil? Is ray.Instance set to the correct part? Is ray.Position set to the correct value?
Printing ray
, the ray hits the part in front of the npc.
That means it works. Phew. So, it must be the part when the position of the attachment is set.
To quickly note, the ViewpointAttachment
is placed just in front of the head of the enemy. Do you think that this has to do with the attatchment itself?
I’m not sure. Have you tried removing it / setting parent to nil?
I was able to find an easier, yet less effective solution by repeatedly tweening the attachment’s orientation so it scans the area in front of it. It may not work well in the long run, but It’s enough for me. Thank you for helping me out as much as you were able to, though!
1 Like