Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i make it so that a model is only visible when the player stands on front of it?

im making a non euclidean game and im having difficulty of how to do this. any effort is ok as long as i can do this.

2 comments

0

maybe create a region3 area in front of the NPC?

0

Have you tried making a extra part and using touch functions?

1 answer

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.

CFrame.fromMatrix ( Vector3 14 pos, Vector3 14 vX, Vector3 14 vY, Vector3 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.

function lookAt(target, eye)
	local forwardVector = (eye - target).Unit
	local upVector = Vector3.new(0, 1, 0)
	-- You have to remember the right hand rule or google search to get this right
	local rightVector = forwardVector:Cross(upVector)
	local upVector2 = rightVector:Cross(forwardVector)
	return CFrame.fromMatrix(eye, rightVector, upVector2)
end

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.

local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")

local ModelName = "" -- name of your model
local Model = workspace[ModelName]

wait(3)

local Character = Player.Character
local found = false


local TweenService = game:GetService("TweenService")

function checkDist(part1, part2)
	return (part1.Position - part2.Position).Magnitude
	
end

function getFOV()
		local modelToCharacter = (Character.Head.Position - Model.PrimaryPart.Position).Unit
		local modelLook =  Model.PrimaryPart.CFrame.LookVector
		
		local dotProduct = modelToCharacter:Dot(modelLook)
		
		return dotProduct
	end

function getVisible(x, x0)
	local newinf = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0.5)
	local visibleTween = TweenService:Create(x, newinf, {Transparency = x0})
	
	return visibleTween
end

RunService.RenderStepped:Connect(function()
	local modelFOV = getFOV()
	
	if modelFOV > .5 and checkDist(Character.HumanoidRootPart, Model.PrimaryPart) <= 50 then
		if not found then
			found = true
			
			print("Player is in FOV")
		    local tween = getVisible(Model.PrimaryPart, 0)
		    tween:Play()
		end
	else
		if found then
			found = false
			
		    print("player is not in fov")
		
		    local Tweenzz = getVisible(Model.PrimaryPart, 1)
		    Tweenzz:Play()
		 	
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.