Spy Satellite Code

Hey ya’ll, I’ve been working on a pet project to improve my math and CFrame skills, so I decided to make a spy sattelite. I’d say I’m a pretty advanced developer when it comes to most stuff, it’s just that my advanced math skills are DOGWATER.

So take a look, tell me if you think I could do anything better.

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local PlayersService = game:GetService("Players")

local localPlayer = PlayersService.LocalPlayer

local mouse = localPlayer:GetMouse()

local camera = workspace.CurrentCamera
local parent = script.Parent
local mainFrame = parent.mainFrame

local altitudeLabel = mainFrame.altitudeText
local positionLabel = mainFrame.positionText
local targetFrame = mainFrame.Target
local targetCenter = mainFrame.TargetCenter

local selectParams = RaycastParams.new()

local baseCameraSpeed = 10
local speedX = 0
local speedZ = 0

local maxCameraFOV = 1
local minCameraFOV = 70

task.wait(2)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(0,1500,0)  * CFrame.Angles(math.rad(-90),0,0)

local inputFunctions = {
	[Enum.KeyCode.W] = function(inputBegan)
		if inputBegan then
			speedZ = baseCameraSpeed * -1
		else
			speedZ = 0
		end
	end,
	[Enum.KeyCode.A] = function(inputBegan)
		if inputBegan then
			speedX = baseCameraSpeed * -1
		else
			speedX = 0
		end
	end,
	[Enum.KeyCode.S] = function(inputBegan)
		if inputBegan then
			speedZ = baseCameraSpeed
		else
			speedZ = 0
		end
	end,
	[Enum.KeyCode.D] = function(inputBegan)
		if inputBegan then
			speedX = baseCameraSpeed
		else
			speedX = 0
		end

	end,
} 

local function updateTargetInfo()
	local viewportPoint = camera.ViewportSize / 2
	local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * 2500)
	
	local result = workspace:Raycast(
		ray.Origin, 
		ray.Direction, 
		selectParams
	)

	if result and result.Instance then
		positionLabel.Text = "POSITION: " .. math.round(result.Position.X) .. ", " .. math.round(result.Position.Z)
		altitudeLabel.Text = "TARGET ALT: " .. math.round(result.Position.Y)
		
		if result.Instance.Parent:FindFirstChild("Humanoid") then
			targetFrame.ImageColor3 = Color3.fromRGB(255,0,0)
			targetCenter.BackgroundColor3 = Color3.fromRGB(255,0,0)
		else
			targetFrame.ImageColor3 = Color3.fromRGB(0,0,0)
			targetCenter.BackgroundColor3 = Color3.fromRGB(0,0,0)
		end
	else
		positionLabel.Text = "POSITION: X,Y" 
		altitudeLabel.Text = "TARGET ALT: N/A"
	end
end

local function updateCameraPosition(deltaTime)
	camera.CFrame += Vector3.new((speedX * camera.FieldOfView) * deltaTime, 0, (speedZ * camera.FieldOfView) * deltaTime)
end

local function adjustZoom(forward)
	if forward then
		if camera.FieldOfView < minCameraFOV then
			camera.FieldOfView += 5 --farther
		end
	else
		if camera.FieldOfView > maxCameraFOV then
			camera.FieldOfView -= 5 --closer
		end
	end
end

local function inputProccess(input,gameProccessed,inputBegan)
	if not gameProccessed then
		if inputFunctions[input.KeyCode] then
			inputFunctions[input.KeyCode](inputBegan)
		end
	end
end

UserInputService.InputBegan:Connect(function(input,gameProccessed)
	inputProccess(input,gameProccessed,true)
end)

UserInputService.InputEnded:Connect(function(input,gameProccessed)
	inputProccess(input,gameProccessed,false)
end)

mouse.WheelForward:Connect(function()
	adjustZoom(false)
end)

mouse.WheelBackward:Connect(function()
	adjustZoom(true)
end)

RunService.RenderStepped:Connect(function(deltaTime)
	updateCameraPosition(deltaTime)
	updateTargetInfo()
end)

Image from Gyazo

It’s pretty functional, just gotta add sit to view.

1 Like

Here is a simplified version of the code, with some additional changes and improvements:

--[[
This script controls a spy satellite camera that can be moved around the world and zoomed in and out.
It also displays information about the target under the center of the camera's viewport.
]]

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

-- Initial camera position and orientation
local initialPosition = CFrame.new(0, 1500, 0)
local initialOrientation = CFrame.Angles(math.rad(-90), 0, 0)

local camera = workspace.CurrentCamera
local mainFrame = script.Parent.mainFrame
local altitudeLabel = mainFrame.altitudeText
local positionLabel = mainFrame.positionText
local targetFrame = mainFrame.Target
local targetCenter = mainFrame.TargetCenter
local selectParams = RaycastParams.new()
local baseCameraSpeed = 10
local speedX = 0
local speedZ = 0
local maxCameraFOV = 1
local minCameraFOV = 70

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = initialPosition * initialOrientation

-- Table of functions to execute when certain keyboard keys are pressed or released
local keyboardInputFunctions = {
	[Enum.KeyCode.W] = function(inputBegan)
		if inputBegan then
			speedZ = baseCameraSpeed
		else
			speedZ = 0
		end
	end,
	[Enum.KeyCode.A] = function(inputBegan)
		if inputBegan then
			speedX = baseCameraSpeed
		else
			speedX = 0
		end
	end,
	[Enum.KeyCode.S] = function(inputBegan)
		if inputBegan then
			speedZ = -baseCameraSpeed
		else
			speedZ = 0
		end
	end,
	[Enum.KeyCode.D] = function(inputBegan)
		if inputBegan then
			speedX = -baseCameraSpeed
		else
			speedX = 0
		end
	end,
} 

-- Update the target information (position and altitude) displayed on the main frame
local function updateTargetInfo()
	local viewportPoint = camera.ViewportSize / 2
	local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * 2500)
	
	local result = workspace:Raycast(ray.Origin, ray.Direction, selectParams)

	if result and result.Instance then
		positionLabel.Text = "POSITION: " .. math.floor(result.Position.X) .. ", " .. math.floor(result.Position.Z)
		altitudeLabel.Text = "TARGET ALT: " .. math.floor(result.Position.Y)
		
		if result.Instance.Parent:FindFirstChild("Humanoid") then
			targetFrame.ImageColor3 = Color3.