What do you want to achieve?
I’m trying to make a 3rd Person camera system similar to the one that this game has:
Stars Align Engine Demo
What is the issue?
I’m not sure that my code is well optimized and I want to add camera collision so it prevents from going through walls or other obstacles, And I don’t know how to do that…
What solutions have you tried so far?
I’ve been searching on Devforum, YouTube, Reddit… And couldn’t find anything related to my issue.
This is my Code so far:
--|| SERVICES ||--
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
--|| VARIABLES ||--
local Camera = workspace.Camera
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CameraAngleX, CameraAngleY = 0, 0
local CameraCFrame, CameraFocus, StartCFrame
local CameraOffset = Vector3.new(0, .25, 20)
local Movement = Enum.UserInputType.MouseMovement
local isCursorEnabled = false -- Track cursor visibility state
local mouse = Player:GetMouse()
--|| INITIAL CAMERA SETUP ||--
Camera.CameraType = Enum.CameraType.Scriptable -- Start with the camera in scriptable mode
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- Lock the cursor in the center
UserInputService.MouseIconEnabled = false
--|| CAMERA UPDATES ||--
RunService.RenderStepped:Connect(function()
if Camera.CameraType ~= Enum.CameraType.Scriptable then
Camera.CameraType = Enum.CameraType.Scriptable
end
StartCFrame = CFrame.new(HumanoidRootPart.Position) * CFrame.Angles(0, math.rad(CameraAngleX), 0) * CFrame.Angles(math.rad(CameraAngleY), 0, 0)
CameraCFrame = StartCFrame:ToWorldSpace(CFrame.new(CameraOffset.X, CameraOffset.Y, CameraOffset.Z))
CameraFocus = StartCFrame:ToWorldSpace(CFrame.new(CameraOffset.X, CameraOffset.Y, -10000))
Camera.CFrame = CFrame.new(CameraCFrame.Position, CameraFocus.Position)
end)
--|| TOGGLE MOUSE VISIBILITY ||--
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.UserInputType == Enum.UserInputType.MouseButton2 then -- Right Mouse Button
isCursorEnabled = not isCursorEnabled -- Toggle cursor state
if isCursorEnabled then
UserInputService.MouseBehavior = Enum.MouseBehavior.Default -- Free cursor
UserInputService.MouseIconEnabled = true
--mouse.Icon = "rbxasset://textures/Cursor.png" -- Show cursor icon
else
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- Lock cursor
UserInputService.MouseIconEnabled = false
--mouse.Icon = "" -- Hide cursor icon
end
end
end)
--|| MOUSE MOVEMENT FOR CAMERA ||--
UserInputService.InputChanged:Connect(function(InputObject)
if InputObject.UserInputType == Movement and not isCursorEnabled then
local Delta = InputObject.Delta
CameraAngleX = CameraAngleX - Delta.X
CameraAngleY = math.clamp(CameraAngleY - Delta.Y, -75, 75)
end
end)
Thanks in advance if someone feels kinda to help me out!