Issue with mouse.Target with custom camera

Hello, i’m a bit rusty, haven’t coded in a while.

I’ve created a “realistic” camera system, it’s in first person and the camera has a limit so it can’t turn over the characters shoulders.

Now the issue is that the mouse.Targer gives a wrong return (it ignores all parts, correctly detects only baseplate or nil). I did a lot of testing and debuging and found out that the problem is with this camera limiter. It seems like the detection is offseted to the side but even then it’s hard to click on an object.

This is the code preventing it from overturning:

local cameraCFrame = char.HumanoidRootPart.CFrame:ToObjectSpace(camera.CFrame)
local x, y, z = cameraCFrame:ToOrientation()
local a = camera.CFrame.Position.X
local b = camera.CFrame.Position.Y
local c = camera.CFrame.Position.Z

local xlimit = math.rad(math.clamp(math.deg(x),-70,40))
local ylimit = math.rad(math.clamp(math.deg(y),-70,70))
local zlimit = math.rad(math.clamp(math.deg(z),-60,60))
camera.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(a,b,c) * CFrame.fromOrientation(xlimit, ylimit, zlimit))

This is not an original code, i’ve found it here on forum and adjusted it a bit. It works perfectly fine.

The camera is set to LockFirstPerson and cameraSubject is set to a certain part in the characters head.

When i disable this code, it works as intended. I’ve been searching the web and the forum for too long and my head hurst at this point.

Hey have you tried using mouse.Hit and raycast yourself instead of using the mouse.Target thing?

2 Likes

I did try it out but in a server script, that didn’t work. Now i’ve put it in a local script in a runService.Stepped function and it works. I hope it won’t be to much to send a ray every frame.

The mouse.Hit doesn’t work as the hit position is offseted a couple studs to the side.

This is the code for a ray:

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {char}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.IgnoreWater = true

ray = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * 200, rayParams)

This is in runService.Stepped function. Then check in mouseButton1Down event the ray.Instance (clicked target). This is a solution for anyone that may have similar issue as me.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.