Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about having the Camera follow the Cursor?

Hello,

I'm trying to make a Script that allows me to lock a Camera on a certain Part, and allow me to look around via the Cursor. I'd like it to stay where it is, and only look so far to the right and above, vice-versa. I am really horrible at mathmatics when it comes to using Objects, logic and whatnot. The best attempt I've gotten led my Camera to appear in void, instead of seeing the original Position, with the Baseplate beneath it. What am I doing wrong, how can I do what I want?

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()
local Camera = workspace.CurrentCamera

Camera.Subject = workspace.CamPart
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CoordinateFrame = workspace.CamPart.CFrame

Cursor.Move:Connect(function()
   Camera.CFrame = Cursor.Hit
end)

-- I realized that Hit was the worldspace CFrame of the Cursor, so this was my attempted fix

Camera.CFrame = CFrame.Angles(math.rad(Cursor.X), math.rad(Cursor.Y), 0)

I'm unavailable to reach the Studio, and I was wondering if my fix would work? If not, how can I do this?

Thanks.

1 comment

0

I have accomplished this in something I made a while back but it does require knowledge in a few advanced mathematical topics.

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use :WaitForChild() to make sure a part exists before changing or using it

You need to wait for the Player's Character to load before their CurrentCamera can be changed if this is not going to be within an activated function.

ISSUES

Use CFrame instead of CoordinateFrame

The "Subject" property of the Camera is named CameraSubject

By changing the CFrame of the Camera to the Mouse's CFrame, the camera will continuously move to the new location of the mouse, essentially giving you a glitch-like behavior.

Instead, use the second property of the CFrame that determines its focus and keep its origin as the "CamPart" (both of these are taken as position vectors within a new CFrame)

REVISED LOCAL SCRIPT

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("CamPart")

repeat wait() until Player.Character.Parent == workspace

Camera.CameraSubject = Cursor
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame

Cursor.Move:Connect(function()
	Camera.CFrame = CFrame.new(part.Position, Cursor.Hit.p)
end)

EDIT: INSIDE FUNCTION (locks movement to be within the borders of the window)

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("CamPart")

repeat wait() until Player.Character.Parent == workspace

Camera.CameraSubject = Cursor
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame

Cursor.Move:Connect(function()
	Camera.CameraSubject = Cursor
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = part.CFrame
	Camera.CFrame = CFrame.new(part.Position, Cursor.Hit.p)
end)

30 comments

0

Right, I'm so dull, in quick addition, would my attempt ever have worked? Also, could you point me in the direction to prevent from looking too far up, down, left, and right? What would setting the CameraSubject to Cursor do?

2

Using repeat wait() to wait for the character isn't very good practice either...

0

@Programmer, your attempt would move the camera, but since everytime you move, the mouse changes location, it would not stay in place, so technically it works, but not in the way it should. To prevent looking too far in a certain direction, you should look at the function math.clamp() and use it to check that cframe position won't move beyond the inclusive borders determined by the clamp

0

@ff, ik, though the script requires the camera be immediately set, not within a function, though i guess the ChildAdded event might work

0

@Programmer math.clamp will work wonders for what you are trying to do

0

I've heard of that, I think I know how to work it, though I'm not sure what the input values should look like, decimals? ranged from 0-1?

0

Could you do ' or Player.CharacterAdded:Wait()'?

0

Don't you have to say 'CoordinateFrame' too?

0

Player.CharacterAdded:wait() or Player.Character is the best approach of getting the character and CFrame is just CoordinateFrame shortened down.

0

No, you can use CFrame, and I'll try with the character added and see. EDIT: Actually if you place the camera's changed properties inside the cursor function it acts like math.clamp() for some reason

0

I can explain to you why character added would be better to use.

0

Could you make a GIF to show me it functional? I'll accept if it is, thanks again!

0

Why would it be?

0

Could you show me what adding the "Cameras changed properties" would look like? Why would it act like that?

0

It's a small puny change but repeat wait() will run wait() even if the character exists. This slows down code but only a small amount so it's not a big deal... Roblox provides events for a reason. They are much faster and more reliable. Not to mention, overusage of wait() can make wait() last seconds.

0

With the code SerpentineKing provided you can easily make something like this https://gyazo.com/25dc73d14747ae63e81bf9443da8138e

0

Inside is making it move only up to the borders of the window, outside allows the camera to do a 360 directional, I'll look and see if i can find why

0

I'm fine with the Inside Fuction, that's all I needed, but how did you do that last one? i like that surpressed movement, how could I make it a bit more widespread though?

0

Hello?

0

I believe he used math.clamp(), it allows you to essentially add boundaries to one's movement, which you can find info about here: https://devforum.roblox.com/t/new-lua-functions-math-sign-and-math-clamp/37309

0

Yes, we discussed this before, why the third-person?

0

Third Person? You mean the Link?

0

I want to know what you did to get the lock on the "Inside Function".

0

I read the message wrong, sorry.

0

Oh ok, I'll put that version on the answer hold on

0

Thanks

0

I used a different method without using math.clamp(). Just did some dot calculations because I wanted the camera to twist a certain way.

0

Could you show me what that looks like?

This discussion is preserved as it was. New answers, comments, and votes are closed.