Lập trình Roblox · Khóa 4: Lập trình nâng cao
workspace.CurrentCamera, chế độ Scriptable| Scratch | Roblox (LocalScript) |
|---|---|
| khi bấm phím [space] | UserInputService.InputBegan |
| phím [shift] được bấm? | UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) |
| (không có) | điều khiển camera |
LocalScript trong StarterPlayerScripts:
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input, dangGoChu) if dangGoChu then return -- đang gõ chat / ô nhập chữ thì bỏ qua end if input.KeyCode == Enum.KeyCode.E then print("Bấm phím E") elseif input.UserInputType == Enum.UserInputType.MouseButton1 then print("Bấm chuột trái") end end)
dangGoChu (gameProcessed): true khi phím được dùng cho chat, giao diệnlocal Players = game:GetService("Players") local UserInputService = game:GetService("UserInputService") local player = Players.LocalPlayer local function datTocDo(tocDo) local nhanVat = player.Character local humanoid = nhanVat and nhanVat:FindFirstChild("Humanoid") if humanoid then humanoid.WalkSpeed = tocDo end end
datTocDo đổi tốc độ nhân vật của chính người chơi này.UserInputService.InputBegan:Connect(function(input, dangGoChu) if not dangGoChu and input.KeyCode == Enum.KeyCode.LeftShift then datTocDo(28) -- bấm Shift: chạy nhanh end end) UserInputService.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then datTocDo(16) -- thả Shift: đi bình thường end end)
local ContextActionService = game:GetService("ContextActionService") local function luot(tenHanhDong, trangThai, input) if trangThai == Enum.UserInputState.Begin then print("Lướt!") end return Enum.ContextActionResult.Sink end ContextActionService:BindAction("Luot", luot, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonX) ContextActionService:SetTitle("Luot", "Lướt")
true: tạo nút trên màn hình điện thoạiContextActionService:UnbindAction("Luot")local camera = workspace.CurrentCamera print(camera.CFrame.Position) -- vị trí camera print(camera.FieldOfView) -- góc nhìn (mặc định 70) camera.FieldOfView = 90 -- nhìn rộng hơn (hiệu ứng tốc độ)
| CameraType | Ý nghĩa |
|---|---|
| Custom | mặc định – camera theo nhân vật |
| Scriptable | camera đứng yên, code toàn quyền điều khiển |
| Fixed | đứng yên một chỗ, vẫn xoay theo người chơi |
local TweenService = game:GetService("TweenService") local camera = workspace.CurrentCamera local diem1 = workspace.CameraDiem1 -- Part vô hình, hướng mặt về cảnh đẹp local diem2 = workspace.CameraDiem2 task.wait(1) camera.CameraType = Enum.CameraType.Scriptable camera.CFrame = diem1.CFrame local bay = TweenService:Create(camera, TweenInfo.new(6, Enum.EasingStyle.Sine), {CFrame = diem2.CFrame}) bay:Play() bay.Completed:Wait() -- chờ bay xong camera.CameraType = Enum.CameraType.Custom -- trả camera cho người chơi
Giữ Shift chạy nhanh (28), thả ra đi bình thường. Khi chạy, FieldOfView tăng lên 85.
Phím Q (và nút trên điện thoại): lướt nhanh về phía trước; hồi chiêu 2 giây.
Khi vào game: camera bay qua 3 cảnh đẹp của bản đồ, có chữ giới thiệu trên màn hình, rồi trả camera cho người chơi. Có nút "Bỏ qua".
| Em gặp | Nguyên nhân |
|---|---|
| Bấm phím không có gì | dùng Script thay vì LocalScript |
| Gõ chat chữ "q" cũng lướt | không kiểm tra dangGoChu (gameProcessed) |
| Camera không nghe lệnh | chưa đặt CameraType = Scriptable |
| Camera kẹt sau đoạn phim | quên trả về Custom |
| Điện thoại không có nút | BindAction thiếu tham số true |
workspace.CurrentCamera: CFrame, FieldOfView, CameraType