Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to make block disappear if they're far away from the player?

Asked by 5 years ago

Hello, I'm a Roblox developer and I have a question. Is it possible to make a script to limit the view distance of the player? By that, I mean blocks that are far away from the player are disabled to improve the framerate. I haven't seen anyone do it so that's the reason why Im asking it. Thank you for your time

0
im sure thats a thing but idk how to do it sorry starmaq 1290 — 5y

1 answer

Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

Yes, you can use the WorldToScreenPoint function to get how far away and where a certain point is in relation to the viewpoint.

However, first consider if this is even necessary. Roblox handles all the necessary rendering for you. If it's just static parts you'd hide, then you do not need to do anything about it. If those parts are in a constant update state of some sort -- was it moving or listening to events -- then you may want to disable this behaviour if the part is out of the player's sight.

For example, I had a project where the player could move the camera freely around a planetary system. There were twenty planets moving around constantly and each could potentially have had dozens of constructions built on them. Nothing was Roblox physics driven, the constructions and planets were moved every frame by my code. There was no way the player could see everything at once, so I wrote a bit of code to not update these models if they were not in the player's view:

function ClassRenderer:start()
    client.services.RunService:BindToRenderStep("renderer", 1, function()
        local screenCenterPoint = camera.ViewportSize / 2
        local visiblePlanets = {}

        table.doToAll(self.classes.Planet, function(planet)
            local doDraw = false            

            local position, visible = camera("WorldToScreenPoint", planet.body.Position)
            local screenPoint, distance = Vector2.new(position.X, position.Y), position.Z
            if not visible then
                local positionDelta = screenPoint - screenCenterPoint
                if distance < -planet.body.Size.X or distance > planet.body.Size.X and positionDelta.Magnitude >= camera.ViewportSize.Magnitude then
                    doDraw = false
                else
                    doDraw = true
                end
            else
                doDraw = true
            end
            planet.cameraDepth = distance

            if doDraw and distance >= SETTINGS.RENDERING.CONSTRUCTION_DEPTH then
                doDraw = false
            end

            drawPlanetConstructions(planet, doDraw)
        end)
    end)
end

It did indeed improve the framerate a lot. I did the same thing with hundreds of projectiles and the demo ran smoothly 60 fps on my laptop.

The reason this works is not because the objects are no more rendered (as Roblox takes care of optimizing that) but because it drops the calculations that are being done potentially tens of thousands of times per second -- so if your code is not doing that, it's not worth it.

Ad

Answer this question