Alright, so I have a moving model in my game. Which is called A320CFM inside it there are diferent cameras and seats which activates an UI to toggle cameras. In my case the camera activates and places the camera on the location but it doesn’t move when the model does.
UI BTN (toggles camera position)
local Tail = workspace.A320CFM.PlaneKit.Plane.OtherParts:WaitForChild("Tail")
script.Parent.MouseButton1Click:Connect(function()
if workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable then
workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
else
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.CurrentCamera.CameraSubject = Tail
end
end)
In its most primitive form, your code could look something like this:
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local cameraPart = workspace.Part
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
RunService.RenderStepped:Connect(update)
Replace the cameraPart variable with your desired part.
Alright, after testing it. It didn’t work, the model just move and the camera stays on the first position the model was when the camera activated.
The code I used:
local Tail = workspace.A320CFM.PlaneKit.Plane.OtherParts:WaitForChild("Tail")
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local cameraPart = Tail
script.Parent.MouseButton1Click:Connect(function(update)
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
RunService.RenderStepped:Connect(update)
end)
Tail is a part, that’s why I directly said local tail = workspace.A320CFM.PlaneKit.OtherParts:WaitForChild("Tail), meaning it will search inside that Model the specific part.
Using WaitForChild() does not necessary imply the instance is a part, hence why I inquired. You also do not need to pass ‘update’ in your anonymous function since the update function is within the scope of the RenderStepped connection. Other than that, I don’t know what to recommend - I’ve tried this using BodyForce vectors and it seems to work perfectly fine.
In my case I also tried with CameraSubject since I already saw a topic related, but it seems it doesn’t work. I don’t know a lot about Camera since I never used it before, but if you can recomend me a good solution I’m free to try it.
I don’t know much about fully customisable cameras but the code I provided will ‘lock’ the camera to a CFrame. As the part moves, the camera should too. Using currentCamera.CFrame with RunService is the only way I know how to manipulate the camera - whether its the most efficient I cannot say. Do you know much about CFrames and the Roblox Matrix?
Probally when I edited the code it break, since you provided me a code without the ClickDetector meaning I probally attached the code in to a wrong side. And no sadly I am not used in to code so I don’t know about Roblox Matrix.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local currentCamera = workspace.CurrentCamera
local cameraPart = workspace.Part
local function fire()
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
RunService.RenderStepped:Connect(update)
end
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
print("Setting Camera")
fire()
end
end)
I tried this and it worked for me (even when I moved the part).
Added this one in to the local script located at the texbutton and nothing, it didn’t even put the camera at the position it is. And yes I changed the cameraPart, console log says nothing.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local currentCamera = workspace.CurrentCamera
local cameraPart = workspace.A320CFM.PlaneKit.Plane.OtherParts.Tail
local function fire()
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
RunService.RenderStepped:Connect(update)
end
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
print("Setting Camera")
fire()
end
end)
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local button = script.Parent
local cameraPart = workspace.Part
local function fire()
print("Fired")
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
RunService.RenderStepped:Connect(update)
end
button.MouseButton1Click:Connect(fire)
Are you getting any errors in the Output window? If you can please send a video and an image of the ‘Tail’ instance in the Explorer window.
Alright! Now it does work, in the case I want to stop it would be:
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local button = script.Parent.Parent.FixCam
local cameraPart = workspace.Part
local function fire()
if Enum.currentCamera.CameraTy == Enum.currentCamera.CameraType.Scriptable then
Enum.currentCamera.CameraType == Enum.currentCamera.CameraType.Custom
end
button.MouseButton1Click:Connect(fire)
```
Or it would it be with RenderStepped?
If you wish to stop the event use the :Disconnect() function. Here is a quick example which I am sure you could modify to suit your needs.
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local button = script.Parent
local cameraPart = workspace.Part
local function fire()
print("Fired")
local function update()
currentCamera.CFrame = cameraPart.CFrame
end
local call = RunService.RenderStepped:Connect(update)
wait(1)
call:Disconnect(update)
print("time")
end
button.MouseButton1Click:Connect(fire)