Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I prevent the camera from rotating on every axis except the Y axis?

Asked by 6 years ago
01wait()
02 
03local Player = game.Players.LocalPlayer
04 
05local Camera = game.Workspace.CurrentCamera
06 
07 
08 
09Camera.CameraSubject = script.Parent.Head
10 
11Camera.CameraType = "Attach"

I like that using cameratype:"attach" works well by locking one axis, but how can I just have the camera rotate on the y axis?

0
So you don't want the player to lift his head up and down, essentially? BlueGrovyle 278 — 6y
0
right. mantorok4866 201 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

You can’t use the “Attach” type, you have to use “Scriptable”. Here’s the code I’m using for the game I’m currently working on. It’s probably not quite what you’re doing, but you should be able to adapt it easily enough:

01-- Player camera script
02-- By TerminusEstKuldin
03 
04-- Services
05local userInputService = game:GetService("UserInputService")
06local runService = game:GetService("RunService")
07-- Player & Camera
08local player = game:GetService("Players").LocalPlayer
09local camera = game:GetService("Workspace").CurrentCamera
10-- Models
11local character = player.Character
12local head = character:WaitForChild("Head")
13local playerPos = head.Position
14-- Camera values
15local cameraRotation = Vector2.new(0,math.rad(-60))
16local cameraOffset = CFrame.new(12, 20, 0)
17-- Set camera type
18camera.CameraType = Enum.CameraType.Scriptable
19 
20-- Refresh camera every frame
21runService.RenderStepped:Connect(function()
22if character and playerPos then
23  -- Start at player position
24  playerPos = head.Position
25  -- Rotate CFrame by cameraRotation value
26  local startCFrame = CFrame.new(playerPos)*CFrame.Angles(0, cameraRotation.X, 0)
27  -- Offset CFrame by defined offset
28  local camPos = startCFrame:ToWorldSpace(cameraOffset)
29  -- Assign attributes to camera
30  camera.CFrame = CFrame.new(camPos.Position, playerPos)
31  camera.Focus = CFrame.new(playerPos)
32  camera.CameraSubject = head
33  end
34end)
35    
36-- Function to check user input to pan camera
37function input()
38-- Check if right mouse button is pressed
39  local pressed = userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)
40  if pressed then
41    -- Lock mouse on screen
42    userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
43    -- Adjust cameraRotation by the value of mouse movement
44    local rotation = userInputService:GetMouseDelta()
45    cameraRotation = cameraRotation + rotation * math.rad(.25)
46  else
47    -- Unlock mouse
48    userInputService.MouseBehavior = Enum.MouseBehavior.Default
49  end
50end
51 
52-- Listen for user input
53userInputService.InputBegan:Connect(input)
54userInputService.InputChanged:Connect(input)
55userInputService.InputEnded:Connect(input)

So what this does is first script the camera to be updated every frame, but only adjusts it by the X value of the camera rotation, not the Y value. It then sets up the InputService to listen for the right mouse button to be pressed and then records the amount the mouse moves to be turned into an equivalent camera rotation.
Depending on where you want your camera to view from, you just have to change the CFrame of your camera.

0
I changed the offset to (0,20,0) which gives me an eagle eye perspective whic his part of what i was looking for, but I cant move the character I can only jump. mantorok4866 201 — 6y
0
Edit: I changed it to (0.1,20,0) and not moving was fixed. mantorok4866 201 — 6y
Ad

Answer this question