How to raycast to see if something is blocking a monsters field of vision

Hello I am trying to do a raycast to see if a monster can see a player or if something is in the way.

here is my code

local rayOrigin = monsterHumanoidRootPart.Position
local rayDirection = Vector3.new(0, -100, 0) -- code here to put direction of player
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

how do I make a racyast to see if a monster can see the player? I think all I have to do is put the correct direction but I dont know how to get the direction;

2 Likes

If you meant as in the direction of their characters. Use HumanoidRootPart.CFrame.LookVector which gives about exactly one stud away frontally.

If you meant as in the direction of their camera, it requires a little more effort by getting the player to send the CFrame of it and the same thing as above. If it is tied to a first-person camera, use the Head.CFrame.LookVector.

After you get this one LookVector, multiply it by a number for its length.

I am just trying to see if the monster is able to see the player.

Are you saying its possible for the monster to have a camera?

this might work better than as it shows everything the monster sees rather than going off numbers. however I strongly doubt that you meant that.

I think I have mistaken. Let me correct this.

So if you have a monster and you want to raycast between it and the player. I’d assume you want (Monster.Head - PlayerCharacter.Head).Magnitude or something along those lines. Or was it Vector3.Unit?

Hello I am trying to fire a raycast from the monster humanoidrootpart and toward the player’s humanoidrootpart. I then want to see if something is in the way.

Here is my code so far:

local rayOrigin = plr.Character.HumanoidRootPart.Position
local rayDirection = plr.Character.HumanoidRootPart.CFrame.LookVector
local raycastRelsult = workspace:Raycast(rayOrigin, rayDirection)

something I think I need to do is add params and then add the parts of the monster to the blacklist so that it doesnt count the monster as a part that gets hit however I forgot how to do this

let me know if you think of soemthing.

Here is how it works with Unit.

local rayOrigin = plr.Character.HumanoidRootPart.Position
local rayDirection = (plr.Character.HumanoidRootPart.Position - Monster.HumanoidRootPart.Position).Unit * length
local raycastRelsult = workspace:Raycast(rayOrigin, rayDirection)

Also for inclusions and exclusions, try consulting the wiki first before you go back here.

I’m not sure on what “length” refers to.

Length refers to the length of the intended raycast. Otherwise you would have a 1-stud ray.

I don’t know the length of the raycast because the player is not going to be in the same length eveyrtime.

Then I’d assume you want the exact distance between them every time, disregarding distance and having some marginal just in case.

local rayOrigin = plr.Character.HumanoidRootPart.Position
local rayTargetVector3 = (plr.Character.HumanoidRootPart.Position - Monster.HumanoidRootPart.Position)
local rayDirection = rayTargetVector3.Unit * (rayTargetVector3.Magnitude + 5)
local raycastRelsult = workspace:Raycast(rayOrigin, rayDirection)
1 Like

The raycast sometimes works and sometimes doesnt.

From what I see there is something in the way between this monster and me however it still says the monster can see me

image

also my pathfinding is not working at all but thats not your problem

another option would be creating a hitbox, which is attached/welded to the Monsters HumanoidRootPart. With that you could detect the walls

the entire point of raycasting is to tell if there is something in the way. why would I add a hitbox when thats the entire purpose of the raycast

I mean it could make everything simple :smile:

it would be more complicated to have to manage a hitbox for detecting when a wall is in the way as I would have to manage the hitbox as well as the raycast