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 5 years ago
wait()

local Player = game.Players.LocalPlayer

local Camera = game.Workspace.CurrentCamera



Camera.CameraSubject = script.Parent.Head

Camera.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 — 5y
0
right. mantorok4866 201 — 5y

1 answer

Log in to vote
1
Answered by 5 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:

-- Player camera script
-- By TerminusEstKuldin

-- Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
-- Player & Camera
local player = game:GetService("Players").LocalPlayer
local camera = game:GetService("Workspace").CurrentCamera
-- Models
local character = player.Character
local head = character:WaitForChild("Head")
local playerPos = head.Position
-- Camera values
local cameraRotation = Vector2.new(0,math.rad(-60))
local cameraOffset = CFrame.new(12, 20, 0)
-- Set camera type
camera.CameraType = Enum.CameraType.Scriptable

-- Refresh camera every frame
runService.RenderStepped:Connect(function()
if character and playerPos then
  -- Start at player position
  playerPos = head.Position
  -- Rotate CFrame by cameraRotation value
  local startCFrame = CFrame.new(playerPos)*CFrame.Angles(0, cameraRotation.X, 0)
  -- Offset CFrame by defined offset
  local camPos = startCFrame:ToWorldSpace(cameraOffset)
  -- Assign attributes to camera
  camera.CFrame = CFrame.new(camPos.Position, playerPos)
  camera.Focus = CFrame.new(playerPos)
  camera.CameraSubject = head
  end
end)

-- Function to check user input to pan camera
function input()
-- Check if right mouse button is pressed
  local pressed = userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)
  if pressed then
    -- Lock mouse on screen
    userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
    -- Adjust cameraRotation by the value of mouse movement
    local rotation = userInputService:GetMouseDelta()
    cameraRotation = cameraRotation + rotation * math.rad(.25)
  else
    -- Unlock mouse
    userInputService.MouseBehavior = Enum.MouseBehavior.Default
  end
end

-- Listen for user input
userInputService.InputBegan:Connect(input)
userInputService.InputChanged:Connect(input)
userInputService.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 — 5y
0
Edit: I changed it to (0.1,20,0) and not moving was fixed. mantorok4866 201 — 5y
Ad

Answer this question