Keep in mind, this is what i interpret as the easiest method.
Calculating The FOV of the model. this may seem confusing.
NEW TOPICS
Vector3.Unit
CFrame.LookVector
UNIT
A vector represents a direction and magnitude (length).
You can think of a vector as an arrow pointing in a certain direction that also has a length, in this case 2 units.
A unit vector is a vector that has a direction and a length of 1 unit, meaning that it only indicates direction
LookVector.
LookVector is a property of CFrames aka Coordinate Frames that represent the unit vector of the CFrame direction. If you’d like to construct your own CFrames with lookvectors, you can call CFrame.fromMatrix.
1 | CFrame.fromMatrix ( Vector 3 14 pos, Vector 3 14 vX, Vector 3 14 vY, Vector 3 14 vZ ) |
Creates a CFrame from a translation and the columns of a rotation matrix. If vz is excluded, the third column is calculated as [vx:Cross(vy).Unit].
This example uses CFrame.fromMatrix() to create a CFrame located at eye with it’s lookVector pointing towards the target position.
1 | function lookAt(target, eye) |
2 | local forwardVector = (eye - target).Unit |
3 | local upVector = Vector 3. new( 0 , 1 , 0 ) |
5 | local rightVector = forwardVector:Cross(upVector) |
6 | local upVector 2 = rightVector:Cross(forwardVector) |
7 | return CFrame.fromMatrix(eye, rightVector, upVector 2 ) |
A more simple explanation;
Look/Up/Right Vectors describe the direction of the Front/Top/Right faces.
Your script
Make a Local Script in StarterCharacterScripts
Give the model a primarypart.
01 | local Player = game.Players.LocalPlayer |
02 | local RunService = game:GetService( "RunService" ) |
05 | local Model = workspace [ ModelName ] |
09 | local Character = Player.Character |
13 | local TweenService = game:GetService( "TweenService" ) |
15 | function checkDist(part 1 , part 2 ) |
16 | return (part 1. Position - part 2. Position).Magnitude |
21 | local modelToCharacter = (Character.Head.Position - Model.PrimaryPart.Position).Unit |
22 | local modelLook = Model.PrimaryPart.CFrame.LookVector |
24 | local dotProduct = modelToCharacter:Dot(modelLook) |
29 | function getVisible(x, x 0 ) |
30 | local newinf = TweenInfo.new( 1 , Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0 , false , 0.5 ) |
31 | local visibleTween = TweenService:Create(x, newinf, { Transparency = x 0 } ) |
36 | RunService.RenderStepped:Connect( function () |
37 | local modelFOV = getFOV() |
39 | if modelFOV > . 5 and checkDist(Character.HumanoidRootPart, Model.PrimaryPart) < = 50 then |
43 | print ( "Player is in FOV" ) |
44 | local tween = getVisible(Model.PrimaryPart, 0 ) |
51 | print ( "player is not in fov" ) |
53 | local Tweenzz = getVisible(Model.PrimaryPart, 1 ) |