Moving character CFrame in relation to part CFrame

As title specifies. Working on a ledge grab feature where i need to move the HumRoot next to the face of the wall they are facing.

The issue:
Depending on the global orientation of the wall, character seems to be repositioned and reoriented completely differently.
I cannot get my head around it.

Video:

Code:

function movement:ledgeGrab(ray_result, dt) -- under freefall condition
	if self.isClimbing == false then
		self.isClimbing = true

		local part = ray_result.Instance
		local normal = ray_result.Normal

		print("part: " .. part.Name)
		print("normal: " .. tostring(normal))

		-- get humroid root part position
		local CharPosX = Character.HumanoidRootPart.Position.X
		local CharPosY = Character.HumanoidRootPart.Position.Y
		local CharPosZ = Character.HumanoidRootPart.Position.Z

		partPosX = part.Position.X
		partPosY = part.Position.Y
		partPosZ = part.Position.Z

		partSizeX = part.Size.X
		partSizeY = part.Size.Y
		partSizeZ = part.Size.Z

		if normal == self:GetNormalFromFace(part, Enum.NormalId.Right) then
			print("Face RIGHT")

			local newCFrame = CFrame.new(partPosX + partSizeX / 2 + 1.05, (partPosY + partSizeY / 2) - 1.009, CharPosZ) * CFrame.Angles(0, math.rad(90), 0)
		

			Animate.LedgeClimbTrack:Play()
			Animate.LedgeClimbTrack:AdjustSpeed(.25)

			Character.HumanoidRootPart.CFrame = newCFrame
			Character.HumanoidRootPart.Anchored = true

			print("CharPosX: " .. Character.HumanoidRootPart.Position.X)
			wait(.5)
			
			Character.HumanoidRootPart.Anchored = false

			--self:ledgeClimb(ray_result, dt)
			Animate.LedgeClimbTrack:Stop()

		end
	end
end

You are offsetting the player from the part in world space, so the script will only work in a specific direction.

Assuming every grab-able ledge in your game is a part with its “Top” face facing up (like the ledges in your video), then the solution is pretty simple:

local rayHit = ray_result.Position
local newPos = Vector3.new(rayHit.X, partPosY + partSizeY / 2, rayHit.Z) -- position in world space
local offset = CFrame.new(0, -1.009, 1.05) -- offset in object space
local newCFrame = CFrame.lookAlong(newPos, -normal) * offset
-- CFrame.lookAlong(newPos, -normal) is equal to CFrame.lookAt(newPos, newPos - normal)
1 Like

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