What is a RAYCAST?

Hello, I am trying to determine if a monster can see the player or not. I’m doing this via rayacast.

here is my raycast code:

local function SeeHunt()
	while true do
		task.wait(.5)
		for i,v in pairs(game.Players:GetPlayers()) do
			
			local orgin = monster.Eyes.Position
			local direction = v.Character.HumanoidRootPart.Position - monster.Eyes.Position
			local params = RaycastParams.new()
			params.FilterDescendantsInstances = {monster.Eyes}
			params.FilterType = Enum.RaycastFilterType.Blacklist

			local result = workspace:Raycast(orgin, direction, params)
 
			if result.Instance == v.Character.HumanoidRootPart then
				print("The monster can see the player")
			else
				print(result.Instance)
			end
		end
	end
end

SeeHunt()

the issue with this code is that the raycast never hits the players humanoidrootpart. It keeps hitting something else like the players tool or something

How do I accurately determine if the monster can see the player?

3 Likes

Check if the Ray is being Intersected by something, then Check if the Intersected Object has a Humanoid

1 Like

well what if the intersected thing doesnt have a humanoid but is still not big enough to block the player? like small block which doesn’t conceal the player completely or a tool they are holding? wont it just cancel the raycast and be counted as “can’t see?” thats not accurately determining if the monster can see the player

1 Like

what if you blacklist thet ool

do a Ray for every character part

1 Like

Since this topic name is “What isa RAYCAST?” here ya go!

In Roblox, a “ray” is a line that starts at a specific point and extends in a specific direction. Raycasting is the process of using rays to detect the presence of objects in the game world.

To use raycasting in Roblox, you can use the Ray class, which is part of the PhysicsService service. The Ray class has several properties and methods that allow you to create and manipulate rays, as well as test for the presence of objects in the game world.

Here is an example of how you can use raycasting to detect the presence of objects in a Roblox game:

local PhysicsService = game:GetService("PhysicsService")

-- define the starting point and direction of the ray
local startPoint = Vector3.new(0, 0, 0) -- the starting point of the ray
local direction = Vector3.new(0, 0, 1) -- the direction the ray is pointing

-- create a new Ray object
local ray = Ray.new(startPoint, direction)

-- use the Ray/Intersect method to test for the presence of objects
local hit, hitPosition, hitNormal = PhysicsService:Intersect(ray)

-- if the ray hits an object, print the position and normal of the hit point
if hit then
  print("Hit position:", hitPosition)
  print("Hit normal:", hitNormal)
end

This code creates a new Ray object with a starting point at (0, 0, 0) and a direction of (0, 0, 1) . It then uses the PhysicsService:Intersect method to test for the presence of objects along the ray. If the ray hits an object, the hit variable will be true , and the hitPosition and hitNormal variables will contain the position and normal of the hit point.

You can use raycasting in a variety of ways in your Roblox game, such as detecting the presence of obstacles for AI units, detecting the position of the mouse cursor for player interaction, or creating weapons that fire rays to damage objects.

I hope this helps! Let me know if you have any questions or need further assistance.

You really shouldn’t post this kind of topic here if u are asking what is a raycast.

2 Likes

Get the size of intersected thing and check if it’s smaller than a certain value.
If it is smaller than certain value, blacklist it.

Get if the intersected thing’s parent is a tool, if it is, blacklist it.

Hello, i’m new to using raycasting, and how do i use it? I need some examples
For example,

  1. determine the distance from the origin of the beam to an intersecting part and get its name, distance, instance, position and material.
  2. Constantly make a raycast operation for live updates on objects (e.g in a loop)
  3. Make the “RayDirection” value be the same as a BasePart/Attachment’s direction

I made a script for an eye monster, it detects if the eye is in the screen of the player, and a raycast to detect if there is anything in the front of the player and the eye

--Local script
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")

local EyeParent = Workspace:WaitForChild("Eye"):WaitForChild("Eye")
local eye = EyeParent:WaitForChild("NewEye")
local OriginalEyePos = eye.Position --34.228, 7, -5.5

local MaxDistance = 100

local TS = game:GetService("TweenService")
local TInfo = TweenInfo.new(
	0.1, --The Tween will take 4 seconds.
	Enum.EasingStyle.Linear, --The tween will use the "Sine" EasingStyle.
	Enum.EasingDirection.In, --The tween will use the "In" EasingDirection.
	0, --The tween will repeat 5 times, so it will perform 6 times in total.
	false, --The tween reverses.
	0 --The tween will delay for 1 second before repeating.
)
local TInfo2 = TweenInfo.new(
	1, --The Tween will take 4 seconds.
	Enum.EasingStyle.Quad, --The tween will use the "Sine" EasingStyle.
	Enum.EasingDirection.In, --The tween will use the "In" EasingDirection.
	0, --The tween will repeat 5 times, so it will perform 6 times in total.
	false, --The tween reverses.
	0 --The tween will delay for 1 second before repeating.
)
local TweenGoals = {
	Size = Vector3.new(0.7,0.7,0.7);
	Color = Color3.fromRGB(33, 10, 10)
}
local TweenGoals2 = {
	Size = Vector3.new(1,1,1);
	Color = Color3.fromRGB(0, 0, 0)
}
local EyeVisible = TS:Create(eye,TInfo,TweenGoals)
local EyeNotVisible = TS:Create(eye,TInfo2,TweenGoals2)

IsVisibleToggle = false




local part1 = EyeParent:WaitForChild("Part1")
local part2 = EyeParent:WaitForChild("Part2")
local random = Random.new()

function EyeMove()
	while IsVisibleToggle do
		wait()
		local PosX = random:NextNumber(part1.Position.X,part2.Position.X)
		local PosZ = random:NextNumber(part1.Position.Z,part2.Position.Z)

		local NewEyePos = Vector3.new(PosX,
			eye.Position.Y,
			PosZ)
		--print(NewEyePos,PosX,PosZ)
		eye.Position = NewEyePos
		--print("Loop")
	end
end

function EyeToPosStart()
	wait()
	eye.Position = OriginalEyePos
	--print("OriginalPos")
end


local Rope = workspace:WaitForChild("RayTesting")
local Att0 = workspace:WaitForChild("Att0Parent"):WaitForChild("Attachment")
local Att1 = workspace:WaitForChild("Att1Parent"):WaitForChild("Attachment")



local TempEvent = game.ReplicatedStorage:WaitForChild("TempEvent")

local RayStart = EyeParent.Parent:WaitForChild("RayStart")

RunService.RenderStepped:Connect(function()
	local camera = workspace.CurrentCamera
	local Vector, inViewport = camera:WorldToViewportPoint(eye.Position)
	--print(Vector)
	--local ray = camera:ViewportPointToRay(Vector.X, Vector.Y, 0)
	--print(ray.Origin,RayStart.Position)
	--print(ray)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {EyeParent,
		game.Players.LocalPlayer.Character,eye,EyeParent.Parent,
		RayStart,Att1.Parent,Att0.Parent}
	
	local direction = game.Players.LocalPlayer.Character.Head.Position - RayStart.Position
	local raycastResult = workspace:Raycast(RayStart.Position,direction, raycastParams)
	--ray.Direction * 1000
	local onScreen = inViewport and Vector.Z > 0
	--print(game.Players.LocalPlayer.Character.Head.Position,RayStart.Position,raycastResult)
	--local onScreen = inViewport and Vector.Z <= MaxDistance
	TempEvent:FireServer(game.Players.LocalPlayer.Character.Head.Position,RayStart.Position)

	--Att0.Parent.Position = ray.Origin
	--Att1.Parent.Position = ray.Direction * 1000
	--Rope.Length = raycastResult.Distance

	local isVisible = onScreen and not raycastResult
	print(isVisible,onScreen,not raycastResult)
	if isVisible then
		if IsVisibleToggle == false then
			EyeVisible:Play()
			IsVisibleToggle = true
			--EyeMove()
		end
		--print("facing a part")

	else
		if IsVisibleToggle == true then
			EyeNotVisible:Play()
			IsVisibleToggle = false
			--EyeToPosStart()
		end

	end
	if raycastResult ~= nil then
		print(raycastResult.Instance)
	end
	--print(raycastResult.Normal)
	--print(raycastResult.Distance)
end)

maybe you are looking to do something like this

1 Like

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