Help with using RenderStepped for Camera Movement

I followed one of the Forum topics here about using RenderStepped for Camera Movement, the script works, however I have multiple camera position, how would I use it for more than 1 camera? I already did an if and else statement and it didn’t really worked or maybe I just did something wrong? :sob:

RunService.RenderStepped:Connect(function()
	local maxTilt = 10
	if CurrentCam.CFrame == Cam1.CFrame then
		CurrentCam.CFrame = Cam1.CFrame * CFrame.Angles(
			math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -maxTilt),
			math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -maxTilt),
			0
		)
		elseif CurrentCam.CFrame == Cam2.CFrame then
		CurrentCam.CFrame = Cam2.CFrame * CFrame.Angles(
			math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -maxTilt),
			math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -maxTilt),
			0
		)
	end
end)
3 Likes

What is Cam1 and Cam2 variables? Objects in your workspace?
That scripts seems that binds the movement of mouse to slightly change the player’s camera rotation and its fixed to the Cam1.CFrame position.

But, when you want to include a “2nd camera” you mean that you want to stop using the Cam1 position and use only the Cam2 position to handle the pivot of the rotation? Like switching from one to another?

1 Like

yes, thats right I want to be able to use both camera positions to be able to move my mouse.

and yes they are Objects/Parts in my Workspace.

Would it be possible to use multiple camera at once with RenderStepped?

I think, depends on what exactly you mean by “at once”.
You can use multiple camera positions and shift the player’s cam to those different positions and make them able to rotate with the mouse movement. But, when you say “use the cameras at once” I kinda understand using all positions of all cameras at the same time. Which obviously makes no sense hehe.

Just create a function that handle which camera you want to “use” in your RenderStep. I mean a way you provide for the user to shift cameras to the next one or choose one in particular, depending on your goal and game mechanics.
I dont know whats the mechanics you are aiming for, maybe somekind of surveillance cameras system?

1 Like

oh sorry for the confusion :sob: I only want to use 1 camera at a time per selection on UI’s since when I hover over my UI, I switch over a different screen, and its a bit bland without the camera moving at all.

Something like this

-- List of all cams
local Cams = {
	workspace:WaitForChild("Cam1"),
	workspace:WaitForChild("Cam2"),
	workspace:WaitForChild("Cam3"),
}

-- Variable holding the current cam to use
local CamToUse = false

-- Choosing a random cam from array
local function ChangeCam()
	CamToUse = Cams[math.random(1,#Cams)]
end

RunService.RenderStepped:Connect(function()
	local maxTilt = 10
	CurrentCam.CFrame = CamToUse.CFrame * CFrame.Angles(
		math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -maxTilt),
		math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -maxTilt),
		0
	)
end)

-- Call it when you want to choose a new random cam
ChangeCam()

wait I don’t want it to be random, I want it to be specific to use per gui, how is that possible?

Just instead of calling it random, call a specific entry of your table of cams
CamToUse = Cams[1] -- this is using the first cam on the list

Mine was only an example of the implementation, you can call for any camera you desire, I was showing you that by having a variable that changes holding the camera reference, your RenderStep loop will follow that reference whenever you change it

can I do something like:

ChangeCam(1)

That will return a variable to the ChangeCam to change to that camera to 1?

kinda like this one I did, do you think this will function?

local function ChangeCam(State)
	CamToUse = MainMenuCameras[State]
end

ChangeCam(1) -- I have set this so whenever the ChangeCam gets called, it will
             -- Send a Variable "1" to ChangeCam.
1 Like

okay oh my god that did really help, TYSM!!! <3

1 Like

Glad I did help, feel free to ask for anything else, happy coding!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.