Making player look towards cursor {X Axis}

Hello everyone,

I want to let my player only look to left and right on the X axis for example:

I tried some scripts but that didnt work because my player also was looking up and down

Are you making a 2D game? Or just a top-down 3D game in Roblox, using the default character controller?

If it’s the latter, there’s a couple ways you could do it. You could set the CFrame of the character every RenderStepped, or you could use physics Constraint objects, like AlignOrientation.

Here’s an example that sets the CFrame direction:

local facingDirection = Enum.NormalId.Right
RunService.RenderStepped:Connect(function()
    local charPos = character.PrimaryPart.Position
    local characterScreenPoint = camera:WorldToViewportPoint(charPos )
    facingDirection = mouse.X > characterScreenPoint.X and Enum.NormalId.Right or Enum.NormalId.Left
    
    character.PrimaryPart.CFrame = CFrame.new(charPos, charPos  + Vector3.FromNormalId(facingDirection))
end)

Animation

1 Like

Hello mate! Thank you for replying so it is a 2D game also i have some errors with the

RunService
character
camera
mouse

1 Like

You need to define those variables

local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local mouse = Player:GetMouse()
2 Likes