Making player to look at a part

So I’m trying to make a climbing script, but for the first step of doing that, I need a player to latch onto a wall.

My problem is, I don’t know how to force a player to face a surface. I’ve tried raycasting and using a bodygyro to make them face it, but it lets me tilt away a bit and doesn’t work at all if I’m shift-locked.

I also don’t know how to force the player to cling to a wall either.

Remote.OnServerEvent:Connect(function(Player,Input)
	
	-- Variables --
	local UserID = Player.UserId
	local Char = Player.Character or Player:WaitForChild("Character")
	local Humanoid = Char.Humanoid
	local HumanoidRootPart = Char.HumanoidRootPart
	
	
	-- RAYCASTING STUFF --
	local RayOrigin = HumanoidRootPart.Position
	local RayDirection = HumanoidRootPart.CFrame.LookVector
	
	local IgnoreList = {}
	local CharDescendants = Char:GetDescendants()
	
	for _, v in pairs(CharDescendants) do
		if v:IsA("MeshPart") then
			table.insert(IgnoreList,v)
		end
	end
	
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = IgnoreList
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local RaycastResult = workspace:Raycast(RayOrigin,RayDirection*10,RayParams)
	
	-- Incase it returns nil
	if RaycastResult then
		Laser.Position = Vector3.new(RaycastResult.Position.X,RaycastResult.Position.Y,RaycastResult.Position.Z)
		
		-- Rotation/Gyro
		local ClimbGyro = Instance.new("BodyGyro")
		ClimbGyro.CFrame = CFrame.new(HumanoidRootPart.Position,RaycastResult.Position)
		ClimbGyro.D	= 100
		ClimbGyro.MaxTorque	= Vector3.new(1,1,1)*math.huge
		ClimbGyro.P	= 3000
		ClimbGyro.Parent = HumanoidRootPart
		
		wait(1)
		
		ClimbGyro:Destroy()
	end
	
	
	print(RaycastResult)	
		
end)

A BodyGyro will sort of ‘smoothly’ turn the player, but it’s not always that accurate. Apparently you’re supposed to use an AlignOrientation instead.

You may be able to use CFrame:Lerp() but I’m not sure how well it will work.

Also I’d assume shift locking would overwrite any rotation you try to set the character to, so you may want to disable it or find a way to get around it.

You could try using an invisible truss part that you collide with but then that will use the default Roblox climbing system and you may not be satisfied with it.

Also you might want to do this on the client as the character is normally controlled by the client and so the server physics may not be able to control the character correctly.

1 Like