教程
GTA 5:在 FiveM 中创建脚本镜头并恢复游戏视角

本页目录
这个 FiveM 示例会在角色旁边架设一个固定镜头。只有执行命令的客户端会切换视角。镜头开启时,脚本根据角色的位置计算机位和镜头朝向的目标点。
准备客户端资源
在私有测试服务器上创建 resources/shotcam_demo 资源,放入下面两个文件。fxmanifest.lua 用于在客户端加载镜头脚本。
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'将下面的脚本保存为 client.lua。它创建镜头,设置位置、目标点和视野角度,然后切换画面。
local cam
local function endShot()
if not cam then return end
if GetRenderingCam() == cam then
RenderScriptCams(false, false, 0, false, false)
end
if DoesCamExist(cam) then DestroyCam(cam, true) end
cam = nil
end
RegisterCommand('shotcam', function()
if cam then
endShot()
return
end
if not IsGameplayCamRendering() then return end
local ped = PlayerPedId()
local position = GetOffsetFromEntityInWorldCoords(ped, 2.0, -4.0, 1.7)
local target = GetOffsetFromEntityInWorldCoords(ped, 0.0, 0.0, 0.9)
cam = CreateCam('DEFAULT_SCRIPTED_CAMERA', true)
if not DoesCamExist(cam) then
cam = nil
return
end
SetCamCoord(cam, position.x, position.y, position.z)
PointCamAtCoord(cam, target.x, target.y, target.z)
SetCamFov(cam, 45.0)
RenderScriptCams(true, false, 0, false, false)
end, false)
AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then endShot() end
end)检查固定镜头和视角恢复
在服务器控制台先执行 refresh,再执行 ensure shotcam_demo。在游戏中保持普通视角,走到开阔处,在 F8 控制台输入 shotcam。移动角色时,机位仍应留在原处。再次输入命令,检查能否重新自由转动视角。
重复测试,在脚本镜头激活时从服务器控制台执行 stop shotcam_demo。停止资源和再次执行镜头命令都会调用同一个清理函数。它关闭当前资源的镜头画面,并只销毁该资源创建的镜头。






评论0