Raycasting help

Ah yes, it’s called the normal of the raycastResult. The direction the object the raycast hits is facing. We can just slightly move the camera in the direction of the raycastResult.Normal, but idk how to manipulate the camera.

1 Like

Didn’t work, I ended up having to run a loop on renderstepped (I don’t know why this is the case, but its the solution.

To anyone having my problems, take my code for free. This goes in a localscript inside startercharacterscripts.

local runs = game:GetService("RunService")

runs.RenderStepped:Connect(function(deltaTime)
	local player = game.Players.LocalPlayer
	local character = player.Character
	local head = character:WaitForChild("Head")
	local camera = workspace.CurrentCamera

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {character, camera}
	params.FilterType = Enum.RaycastFilterType.Exclude
	local raycastResult = workspace:Raycast(head.Position, (camera.CFrame.Position - head.Position), params)

	if raycastResult then
		camera.CFrame = (camera.CFrame - (camera.CFrame.Position - raycastResult.Position)) + (character.Head.Position - camera.CFrame.Position).Unit    
	end
end)

Without my script :

With my script :

1 Like

Looks good. One thing you can do is optimized variables that are re-used such as player and params. character, head, and camera are subject to change. And the param’s FilterDescendantsInstances property can be changed inside the function as well.
Another optimization you can do is RunService:BindToRenderStep .
The priority would be 1 more than camera so Enum.RenderPriority.Camera.Value+1 . This makes it so the function runs after the camera update instead of before it. But .RenderStepped:Connect runs functions after all binded functions since it has lowest priority.

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