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