Casting ray from mouse buggy with custom camera

I could REALLY use some help figuring this one out.
So I cast a ray from the mouse into a ‘baseplate’ type part I made just so the ray would hit it. The purpose is to get where the players mouse is pointing to on a specific Y plane.

RunService.RenderStepped:Connect(function()
	local rayOrigin = Mouse.Origin.Position
	local rayDirection = Mouse.UnitRay.Direction * 1000

	local result = workspace:Raycast(rayOrigin, rayDirection, AimRayParams)
	if result then
		AimPart.Position = result.Position
	end
end)

Now this code works fine except when I’m trying to use my custom camera script. Basically it creates a part manually moves that around and sets the camera to it’s cframe every frame. I did this to make a top-down camera and easily do things like screenshake or even camera rotation if i wanted.
My question is WHY does me messing with the camera orientation and position seem to break the ability to use the mouse origin/unitray properly? How are these 2 even interacting?

Here’s the camera runservice code so you can kinda see how it works if that’ll help:

RunService.RenderStepped:Connect(function(dt)
	if Character ~= nil and Character.Humanoid.Health > 0 and Root then
		local shake = ShakeSpring:update(dt)
		local OffX, OffZ = CreateOffset()
		CamPart.Position = Vector3.new((Root.Position.X + OffX)+shake.X, CamHeight, (Root.Position.Z + OffZ)+shake.Z)
		Camera.CFrame = CamPart.CFrame
		local LookAt = Vector3.new(Mouse.Hit.Position.X, Root.Position.Y, Mouse.Hit.Position.Z)
		Root.CFrame = CFrame.lookAt(Root.Position, LookAt)
	end
end)

Fixed it by putting the aimpart stuff in a task.defer(function() i dont fully understand why that fixed it but it did.

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