Last active
October 28, 2022 00:40
-
-
Save mvyasu/ec83c1f528392ec2d257d6e972283648 to your computer and use it in GitHub Desktop.
A simple LocalScript that will hide the player's character when they zoom into the current CameraSubject if it isn't a humanoid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --CameraSubjectLocalTransparency | |
| --by @mvyasu | |
| local HIDE_CHARACTER_WHEN_DISTANCE_IS_LESS_THAN = 5 | |
| local SHOULD_TWEEN_TRANSPARENCY = false | |
| local TRANSPARENCY_TWEEN_INFO = TweenInfo.new(1) | |
| local TweenService = game:GetService("TweenService") | |
| local player = game.Players.LocalPlayer | |
| local camera = workspace.CurrentCamera | |
| local activeConnections = {} | |
| local function cleanupConnections() | |
| local connectionsToCleanup = table.clone(activeConnections) | |
| table.clear(activeConnections) | |
| for _,connection in connectionsToCleanup do | |
| connection:Disconnect() | |
| end | |
| end | |
| local function addConnection(connection: RBXScriptConnection) | |
| table.insert(activeConnections, connection) | |
| end | |
| local function setupCharacter(character) | |
| local wasHiddenLastUpdate = nil | |
| local function shouldBeHidden(): boolean | |
| if camera.CameraType==Enum.CameraType.Scriptable then | |
| return false | |
| end | |
| local cameraSubject = camera.CameraSubject | |
| if cameraSubject then | |
| if cameraSubject:IsA("Humanoid") then | |
| return false | |
| end | |
| local subjectPosition = cameraSubject:GetPivot().Position | |
| local cameraPosition = camera.CFrame.Position | |
| return (subjectPosition-cameraPosition).Magnitude < HIDE_CHARACTER_WHEN_DISTANCE_IS_LESS_THAN | |
| end | |
| return false | |
| end | |
| local function setDescendantTransparency(object, forcedTransparency: number?) | |
| if object:IsA("BasePart") or object:IsA("Decal") then | |
| local newTransparency = forcedTransparency or (if wasHiddenLastUpdate then 1 else 0) | |
| if SHOULD_TWEEN_TRANSPARENCY then | |
| TweenService:Create(object, TRANSPARENCY_TWEEN_INFO, {LocalTransparencyModifier = newTransparency}):Play() | |
| else | |
| object.LocalTransparencyModifier = newTransparency | |
| end | |
| end | |
| end | |
| local function setCharacterTransparency() | |
| for _,descendant in character:GetDescendants() do | |
| setDescendantTransparency(descendant) | |
| end | |
| end | |
| local function updateCharacterTransparency() | |
| local shouldHideCharacter = shouldBeHidden() | |
| if shouldHideCharacter==wasHiddenLastUpdate then | |
| return | |
| end | |
| wasHiddenLastUpdate = shouldHideCharacter | |
| setCharacterTransparency() | |
| end | |
| updateCharacterTransparency() | |
| cleanupConnections() | |
| addConnection(camera.Changed:Connect(updateCharacterTransparency)) | |
| addConnection(character.DescendantAdded:Connect(setDescendantTransparency)) | |
| addConnection(character.DescendantRemoving:Connect(function(descendant) | |
| setDescendantTransparency(descendant, 0) | |
| end)) | |
| end | |
| player.CharacterAdded:Connect(setupCharacter) | |
| if player.Character then | |
| task.spawn(setupCharacter, player.Character) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment