I want to make a raycasting that detects only the front part of the character's body

  1. What do you want to achieve? Keep it simple and clear!

I want to make a raycasting that detects only the front part of the character’s body in the area that it can see.

  1. What is the issue? Include screenshots / videos if possible!

I want the AI to only move when the player is not facing the monster.

And stay still when player facing the monster

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried to add a small part in front of the character but the player only needs to rotate his body 90 degrees and does not need to be 100% facing the monster to stop it, and I want the monster to only chase the player in front of him, because if I use a small part in front of the character then the monster will still be able to detect the player even though it is behind him.

I wanted to make the raycasting like it’s a monster’s vision, so it only sees what’s in front of it, but after reading and watching tutorial videos on youtube I only found out how to make raycasting, blasting a line to a certain part.

and please provide me with useful resources for creating AI, raycasting, and pathfinding, thank you all. :smiley: :heart_hands:

You could use dot products to achieve this:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local playerCharacter = player.Character or player.CharacterAdded:Wait()
local targetMonster = workspace:WaitForChild("Rig")

local playerRootPart = playerCharacter:FindFirstChild("HumanoidRootPart")
local monsterRootPart = targetMonster:FindFirstChild("HumanoidRootPart")

local MAX_ANGLE = 160 -- A value of 180 means they have to be face to face with the monster to detect.

local runConnection = nil
runConnection = RunService.RenderStepped:Connect(function()
	local angleBetween = math.deg(math.acos(monsterRootPart.CFrame.LookVector:Dot(playerRootPart.CFrame.LookVector)))
	
	if angleBetween >= MAX_ANGLE then
		print("Looking at monster")
	end
end)
1 Like

Wow this is the answer to my problem thanks. :saluting_face: :saluting_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.