Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local playerFolder = game.Workspace.Players
- local cockpit = true
- local inpart, outpart
- local UserInputService = game:GetService("UserInputService")
- local RunService = game:GetService("RunService")
- local rs = game:GetService("ReplicatedStorage")
- local player = game:GetService("Players").LocalPlayer
- local mouse = player:GetMouse()
- game.Workspace.CurrentCamera:Remove()
- local camera = Instance.new("Camera",workspace)
- camera.Name = player.Name.."Camera"
- camera.CameraType = Enum.CameraType.Scriptable
- local X, Y = mouse.X, mouse.Y
- local ViewX, ViewY = camera.ViewportSize.X, camera.ViewportSize.Y
- local center = Vector2.new(ViewX/ 2, ViewY / 2)
- local minFOV, maxFOV = 40, 90
- local currentFOV = maxFOV
- local FOVstep = 5
- local radius = 30
- local rotSensetivity = 5
- local maxZRot = 60 -- yaw
- local maxAccel = 50
- local currentAccel = 30
- local isFiring
- local freeview = false
- local currentGun = 1
- local lastAccelTick, lastApplyAccelTick, lastBoltFired = tick(), tick(), tick()
- local w, s, a, d, f, q, e = false, false, false, false, false, false, false
- local changepos = rs.changepos
- local firebolt = rs.firebolt
- function lerp(a, b, t)
- return a * (1-t) + (b*t)
- end
- -- get value from one screen to the other starting from the center
- function calculateValue(axis, valmin, valmax)
- local percentage = mouse[axis] / camera.ViewportSize[axis]
- local valueRange = valmax - valmin
- local calculatedValue = (percentage * valueRange) + valmin
- return calculatedValue
- end
- mouse.WheelForward:Connect(function()
- if freeview then
- if currentFOV >= (minFOV+FOVstep) then
- currentFOV -= FOVstep
- end
- else
- if currentAccel < maxAccel then
- currentAccel += 5
- end
- end
- end)
- mouse.WheelBackward:Connect(function()
- if freeview then
- if currentFOV <= (maxFOV-FOVstep) then
- currentFOV += FOVstep
- end
- else
- if currentAccel > 0 then
- currentAccel -= 5
- end
- end
- end)
- UserInputService.InputBegan:Connect(function(input)
- if input.KeyCode == Enum.KeyCode.C then
- if not cockpit then
- cockpit = true
- camera.FieldOfView = maxFOV
- currentFOV = maxFOV
- else
- cockpit = false
- camera.FieldOfView = maxFOV
- currentFOV = maxFOV
- end
- end
- if input.KeyCode == Enum.KeyCode.Q then
- q = true
- end
- if input.KeyCode == Enum.KeyCode.E then
- e = true
- end
- if input.KeyCode == Enum.KeyCode.W then
- w = true
- end
- if input.KeyCode == Enum.KeyCode.S then
- s = true
- end
- if input.KeyCode == Enum.KeyCode.A then
- a = true
- end
- if input.KeyCode == Enum.KeyCode.D then
- d = true
- end
- if input.KeyCode == Enum.KeyCode.F then
- freeview = true
- f = true
- end
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- isFiring = true
- end
- end)
- UserInputService.InputEnded:Connect(function(input)
- if input.KeyCode == Enum.KeyCode.Q then
- q = false
- end
- if input.KeyCode == Enum.KeyCode.E then
- e = false
- end
- if input.KeyCode == Enum.KeyCode.W then
- w = false
- end
- if input.KeyCode == Enum.KeyCode.S then
- s = false
- end
- if input.KeyCode == Enum.KeyCode.A then
- a = false
- end
- if input.KeyCode == Enum.KeyCode.D then
- d = false
- end
- if input.KeyCode == Enum.KeyCode.F then
- freeview = false
- f = false
- end
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- isFiring = false
- end
- end)
- -- center of screen to edges in pixels
- function getDistanceFromCenter(position)
- return (position - center).Magnitude
- end
- --repeat task.wait() until playerFolder[player.Name] ~= nil
- local lastRotX = 0
- local lastRotY = 0
- local lastRotZ = 0
- function updateCamera()
- --print(currentFOV)
- if playerFolder[player.Name] ~= nil then
- mouse.Icon = "http://www.roblox.com/asset/?id=13734905207"
- local camera = workspace:FindFirstChild(player.Name.."Camera")
- X, Y = mouse.X, mouse.Y
- ViewX, ViewY = camera.ViewportSize.X, camera.ViewportSize.Y
- center = Vector2.new(ViewX/ 2, ViewY / 2)
- local ship = playerFolder[player.Name]
- local inHUD = ship.inHUD
- local accelBar = inHUD.SurfaceGui.Frame.Accel.Accel.bar
- local shipOrigin = ship.Origin
- local shipSounds = shipOrigin.Sounds
- local thrusterSound = shipOrigin.Sounds.thruster
- local blasterSound = shipOrigin.Sounds.blaster
- local shipPosition = shipOrigin.Attachment.AlignPosition
- local shipOrientation = shipOrigin.Attachment.AlignOrientation
- local shipThrusters = {}
- local shipGuns = {ship.leftGun, ship.rightGun}
- -- get both thrusters, put it in a table
- for _, v in pairs(ship:GetChildren())do
- if v.Name == "Thruster" then
- table.insert(shipThrusters, v)
- end
- end
- local serverdata = require(ship.serverdata)
- serverdata.cameralookvector = camera.CFrame.LookVector
- serverdata.cameraupvector = camera.CFrame.UpVector
- local delta = UserInputService:GetMouseDelta()
- local x, y = delta.X, delta.Y
- local turning = false
- local centerTodistance = getDistanceFromCenter(Vector2.new(X, Y))
- local incircle = ViewY/17
- local outcircle = ViewY/2.4
- local rotation = (camera.CFrame-camera.CFrame.Position) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
- --rotation = (camera.CFrame-camera.CFrame.Position) * CFrame.Angles(math.rad((-y)*rotSensetivity),math.rad((-x)*rotSensetivity),math.rad(0))
- --X >= 0 and X <= ViewX and Y >= 0 and Y <= ViewY and
- if camera and ship then
- local xValue = calculateValue("X", -20, 20)
- local yValue = calculateValue("Y", -20, 20)
- camera.FieldOfView = lerp(camera.FieldOfView, currentFOV, 0.2)
- if cockpit then--doesn't need editting
- -- in first person
- camera.CFrame = ship:FindFirstChild("inPart").CFrame * CFrame.Angles(-math.rad(yValue) , -math.rad(xValue) ,0)
- else
- -- in third person
- camera.CFrame = camera.CFrame:lerp(ship:FindFirstChild("outPart").CFrame * CFrame.Angles(-math.rad(yValue) , -math.rad(xValue) ,0), 0.1)
- end
- end
- -- if moving
- if currentAccel > -1 then
- if (tick() - lastAccelTick) > 0.1 then
- local xValue = calculateValue("X", -20, 20)
- local yValue = calculateValue("Y", -20, 20)
- lastAccelTick = tick()
- -- if not moving, stop thrusters
- if currentAccel == 0 then
- thrusterSound:Stop()
- else
- if not thrusterSound.Playing then
- thrusterSound:Play()
- end
- end
- -- if mouse in turning range
- if centerTodistance > incircle then
- turning = true
- rotSensetivity = (centerTodistance-incircle)
- rotation = (camera.CFrame-camera.CFrame.Position) * CFrame.Angles(math.rad(-y),math.rad(-x),math.rad(0))
- else
- rotation = (camera.CFrame-camera.CFrame.Position) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
- turning = false
- end
- local xx, yy, zz = math.rad(0), math.rad(0), math.rad(0)
- -----
- -- pressing q, rotate right
- if q and not e then
- yy = math.rad((-x)+maxZRot)
- end
- -- pressing e, rotate left
- if e and not q then
- yy = math.rad((-x)-maxZRot)
- end
- -----
- -- pressing d, tilt right
- if d and not a then
- zz = math.rad(-maxZRot)
- end
- -- pressing a, tilt left
- if a and not d then
- zz = math.rad(maxZRot)
- end
- -----
- -- pressing w, tilt down
- if w and not s then
- xx = math.rad((-y)-maxZRot)
- end
- -- pressing s, tilt up
- if s and not w then
- xx = math.rad((-y)+maxZRot)
- end
- if a or d or w or s or q or e then
- rotation = (camera.CFrame-camera.CFrame.Position) * CFrame.Angles(xx,yy,zz)
- end
- -----
- -- change thruster particles based on speed
- for _, v in pairs(shipThrusters)do
- if v and v.Flames then
- v.Flames.Rate = (currentAccel/maxAccel)*100
- end
- end
- -- apply changes to the server
- changepos:FireServer(camera.CFrame.LookVector * currentAccel, rotation)
- end
- end
- -- set thruster volume
- thrusterSound.PitchShiftSoundEffect.Octave = lerp(thrusterSound.PitchShiftSoundEffect.Octave,(currentAccel/maxAccel),0.1)
- thrusterSound.Volume = lerp(thrusterSound.Volume, (currentAccel/maxAccel) * 0.15, 0.1)
- --hud speed bar
- accelBar.Size = UDim2.new(1,0,(currentAccel/maxAccel),0)
- -- self explainatory
- if isFiring then
- if (tick()-lastBoltFired) > 0.12 then
- print("firing")
- if currentGun == 1 then
- blasterSound:Play()
- firebolt:FireServer(ship, shipGuns, currentGun)
- currentGun = 2
- lastBoltFired = tick()
- elseif currentGun == 2 then
- blasterSound:Play()
- firebolt:FireServer(ship, shipGuns, currentGun)
- currentGun = 1
- lastBoltFired = tick()
- end
- end
- end
- else
- return warn("no ship")
- end
- end
- RunService.RenderStepped:Connect(function()
- --if playerFolder[player.Name] ~= nil then
- updateCamera()
- --end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement