In island royale, there is a custom camera, type scriptable. When you move your mouse, your camera slightly moves towards it, I am trying to replicate this, but this isn’t working. I got another script that already changes the camera CFrame, so I made a lc in startergui and this error on line 7 keeps happening;
7: bad argument #2 (Vector3 expected, got CFrame)
Here is the code;
camera.CameraSubject = workspace.CamPart
local mouse = game.Players.LocalPlayer:GetMouse()
local posmouse = mouse.Hit
while wait(.1) do
if camera.CameraSubject == workspace.CamPart then
camera.CoordinateFrame = workspace.CamPart.Position -posmouse
--posmouse-workspace.CamPart.Position
end
end
local posmouse = mouse.Hit This is the root of your problem, this returns a CFrame but you’re asking for a Vector. Try mouse.Hit.Position instaid of mouse.Hit.
Another problem I see with your code is that camera.CoordinateFrame requires a CFrame, not a vector. Try workspace.CamPart.CFrame instaid of workspace.CamPart.Position.
Also make sure the part is facing in the right direction. Meaning the ‘Forward’ face of the part must be facing in the direction you want to face in.
CFrames already have a position factored into their value, since it has a CFrame.Position property. The real problem is probably just that you want the direction the mouse is facing but not its position as well. You can just subtract its position, and it should give you what you want:
Actually, this probably wouldn’t work either because it could cause your camera to spin from adding to the orientation every .1 seconds. That depends on your original camera script though.
You could probably use the delta between the camera’s current CFrame and mouse.Hit's orientation to get on the right track:
The problem is the code tries to set the camera rotation to the mouses rotation. But when the camera rotates it also automaticly rotates the mouse.
Instaid of setting it to the mouses rotation each time, try to do newrotation = oldrotation + deltarotation.
You can use GetMouseDelta(). The problem with MouseDelta is that it returns the delta in pixels, you’ll need to change this to rotation.
local uiService = game:GetService("UserInputService")
local SCREEN_RES = workspace.CurrentCamera.ViewportSize
local sens = Vector2.new(2,2) --sensetivity
local starterCFrame = workspace.CamPart.CFrame
function render() --call this each frame, I would recommand: runService:BindToRenderStep()
local cDelta = iuService:GetMouseDelta()
if oDelta.Y == cDelta.Y then
cDelta = Vector2.new(cDelta.X,0)
end
local yD = cDelta.Y/-SCREEN_RES.Y/2*sens.Y --delta into rotation
if camRot <= -1.4 then
camRot = -1.4
elseif camRot >= 1.4 then
camRot = 1.4
end --set a max y rotation so the user can't just flip thier screen
local xD = cDelta.X/-SCREEN_RES.X/2*sens.X
camRot = camRot + Vector2.new(yD,xD)
workspace.CurrentCamera.CFrame = starterCFrame*CFrame.Angles(camRot.X,camRot.Y,0)
end
Ah, how stupid of me! My mistake sorry, after workspace.CurrentCamera.CFrame = and before end put oDelta = cDelta and at the very beginning of the code, near starterCFrame put:
local camRot = Vector2.new() --stores all delta values
local oDelta = Vector2.new() --the previous delta, this is used to check if the mouse is still or not. There's a 'bug' with MouseDelta() that it when you stop moving the mouse it gives out old deltas.
Yep, I’ve put camRot instaid of camRot.Y, it’s common for me to make these small mistakes. Replace all camRot with camRot.Y. Only replace it tho it there is nothing behind it. (aka camRot replace, camRot.X not)
You could use a different method to achieve this effect, and it would actually work a little better.
Instead of checking the mouse’s hit position, you can alter the camera angle based on the mouse’s screen position. This would work across all resolutions and FieldOfViews, so I prefer to use it.
You get (Mouse.X/Mouse.ViewSizeX)-0.5, and this will give you a number between -0.5 and 0.5, and then do the same for the Y.
Once you have that, you can use CFrame.Angles() to alter the camera CFrame based on the mouse position.
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local CamCF = workspace.CamPart.CFrame
game:GetService("RunService"):BindToRenderStep("CameraAngle", 201, function()
local MousePos = Vector2.new((Mouse.X/Mouse.ViewSizeX)-0.5,(Mouse.Y/Mouse.ViewSizeY)-0.5)
Camera.CFrame = CamCF*CFrame.Angles((-MousePos.Y/5),(-MousePos.X/5),0)
end)