Answered by
5 years ago Edited 5 years ago
There is a built in way to do this by using Camera.Focus. It basically prioritizes what to show in full quality based on where the focus currently is. By default, Camera.Focus is always on the Camera.Subject, however if you change the default camera scripts, you can control where the focus is via scripting. So to control the Camera.Focus, you need to set the camera type to scriptable, and you need to override the default Camera scripts. And also, to update it every frame, you can use BindToRenderStep rather than having a while true loop.
So for instance, if you have an fps game, and you want to unrender most of the objects directly behind the player, you can set the focus to be downrange of the player, so that the game renders everything in front of them to what their graphics quality is set at, but leaves out all the lighting and graphics changes behind them.
You can do the same with a 3rd person game, it just becomes a bit more difficult in that you need to use a bit more math when setting the focus in relation to the camera instead of humanoid root part of the 1st person method.
Here's an example of focus following the point in 3D space that the mouse is pointing at:
01 | local UserInputService = game:GetService( "UserInputService" ) |
02 | UserInputService.InputChanged:Connect( function (input, gameProcessed) |
03 | if input.UserInputType = = Enum.UserInputType.MouseMovement then |
04 | local ray 1 = Camera:ScreenPointToRay(input.Position.X, input.Position.Y, 0 ) |
05 | local ray = Ray.new(ray 1. Origin,ray 1. Direction * 1000 ) |
06 | local part, hitPos = workspace:FindPartOnRayWithIgnoreList(ray,character) |
07 | local Focus = CFrame.new(hitPos, |
09 | * CFrame.Angles( 0 ,math.rad( 180 ), 0 ) |
11 | print ( "Mouse moved, Focus is now: " ..Focus.Position.X,Focus.Position.Y,Focus.Position.Z) |
Just click view source on the code block to see the comments better, I've explained what is going on line by line.
I also use the UserInputService rather than render stepped here because if you want it to follow the mouse, then you would want to save some performance by not having it shoot rays every frame if you don't have to.