Raycasting from NPCs Torso

I’ve been trying to raycast to detect when a NPC is approaching a wall within 5 studs. Does this look like the proper way to do it? I have an additional script that involves changing the NPCs walking direction to avoid walls, but for now it’s not seeming to work, and i’d like to know if the problem lies here.

Thanks!

05%20PM

2 Likes

I believe the code you’ve provided is fine. May you maybe post a snippet of a code providing :FindPartOnRay, the issue could lie there.

I feel that you would need Pathfinding Service, as it can do all tihs and more. There is also an Introduction to Pathfinding Video published by Roblox as well

Look here if you want documentation and want to skip tutorials.

I could be wrong but I believe the second value is the direction you’ll be shooting the ray, you’re not specifying a direction at all?

Try and get the NPC’s HumanoidRootPart’s lookVector perhaps?

Constructors

Ray.new ( Vector3 Origin, Vector3 Direction )
Creates a new Ray with given Origin and Direction

That’s probably because you’re doing it wrong. Ray accepts two Vector values; an origin and a direction. You simply need to assign the first value as the HumanoidRootPart’s (not the Torso) position and the second value as 5 studs on the… I think X or Z axis, can’t remember.

I believe that each argument is handled in object space as opposed to world space.

You need to call game.Workspace:FindPartOnRay().

Try something like this

local ray = Ray.new(script.Parent.HumanoidRootPart.Position, Vector3.new(0,0,-5))
--Create a ray that goes 5 studs in front of the character
local Part, Position = game.Workspace:FindPartOnRay(ray, script.Parent)
--Finds if there's a part hit, and where the ray ends, while ignoring the character
if Part then
    --do your stuff here
end
local ray = Ray.new(
    script.Parent.Torso.CFrame.p,
    script.Parent.Torso.CFrame.lookVector * 5
)

That will create a ray starting at the torso going 5 studs in the direction the torso is facing.

1 Like

Here is the remainder of the code. The NPC always follows a part called FollowPart, which is normally located behind and to the side of a player’s torso (Acts like a sidekick to a real player.) When a zombie gets nearby, the sidekick runs directly away from the zombie, which can be see in right below the section “if hit == nil” The code in the else, which should be running when the ray detects a wall should be placing the follow part in a location that makes the sidekick turn. I know that the math in that section is correct because I tested it out with a visible part myself.

Apologies for the late reply.

It appears that you’re doing your raycasting right and the math for running away shall the raycast’s hit be nil. But the math for the turning is incorrect. Sadly, I’m not very experienced with this kind of math so I probably can’t help you further.

https://gyazo.com/771f728d7e7fcef08842007fb51e3154.mp4

As a side note, it’s a good idea when providing long code like the screenshot you sent me as a code block as it is extremely tedious to retype it.

1 Like