Climbing system only works in the Z direction, how to adapt based on HRP's LookVector?

Currently my limbing system only works if the player is facing the Z direction, how would i adapt it to work in any direction?

Here’s my script:

game:GetService("RunService").Heartbeat:Connect(function()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}

	local raycastResult = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector * 1.15, raycastParams)

	wall = raycastResult and raycastResult.Instance or nil
	
	if wall and char:FindFirstChildOfClass("Tool") == nil and Stamina["Amo"].Value > 10 and Roll["Using"] == false then
		-- moving player forward when they try to move back to stick them onto the wall
		local add2 = 0
		if hum.MoveDirection.Z < 0 then
			add2 = math.abs(hum.MoveDirection.Z)*1.5
		end
		
		-- hum.MoveDirection.X to move side to side, while hum.MoveDirection.Z to move up and down
		hrp.Velocity = Vector3.new(hum.MoveDirection.X * 12, hum.MoveDirection.Z * 12 + 2.75 + add, add2)

		if script.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then 
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Climbing) 
		end
		if not isClimbing then
			isClimbing = true
			warn("climbing")
		end
		if hum.MoveDirection.Z == 0 and hum.MoveDirection.X == 0 then
			if climbstill.IsPlaying == false then
				climbstill:Play()
			end
			climb:Stop()
			Stamina:RepStop()
		else
			if climb.IsPlaying == false then
				climb:Play()
				Stamina:RepStart(10,"climb",true)
			end
			climbstill:Stop()
		end
	else
		if add > 0 then
			hrp.Velocity = Vector3.new(hrp.Velocity.X, hrp.Velocity.Y + add/17, hrp.Velocity.Z)
		end
		if isClimbing then
			Stamina:RepStop()
			warn("not climbing")
		end
		isClimbing = false -- character stops climbing
		climbstill:Stop()
		climb:Stop()
	end
end)

I understand that the problem lies in changing the hum.MoveDirection to something that is based off where the player is looking, but I just don’t know how. Please help!

You are checking inputs based on world space coordinates, you could adapt this to use local / object space. I would recommend doing:

local direction = cameraCFrame:VectorToObjectSpace(moveDirection)

Then you can check this direction the same way you did before, but it should work as expected. Putting this in object space relative to the camera means that all inputs will be treated relative to the camera. Z would be forward and backwards, X would be left and right.

I’ve tried two different solutions based on what you’ve given:

local moveDirection = hum.MoveDirection
	
local direction = hrp.CFrame:VectorToObjectSpace(moveDirection)

local add2 = 0
if direction.Z < 0 then
	add2 = math.abs(direction.Z)*1.5
end
	
-- hum.MoveDirection.X to move side to side, while hum.MoveDirection.Z to move up and down
hrp.Velocity = Vector3.new(direction.X * 12, direction.Z * 12 + add, add2)

and

local moveDirection = hum.MoveDirection
		
local add2 = 0
if moveDirection.Z < 0 then
	add2 = math.abs(moveDirection.Z)*1.5
end
	
local direction = hrp.CFrame:VectorToObjectSpace(Vector3.new(moveDirection.X * 12, moveDirection.Z * 12 + add, add2))
	
hrp.Velocity = direction

Both still seem to be influenced by the wall’s direction, any solution? Also, I want it to be based on where the character is facing, not the player, so i replaced camera cframe with the humanoid root part’s cframe.

Problem solved!

local moveDirection = hum.MoveDirection

local direction = hrp.CFrame:VectorToObjectSpace(moveDirection)

local add2 = 0
if direction.Z > 0 then
	add2 = direction.Z*-1.5
end

hrp.Velocity = hrp.CFrame:VectorToWorldSpace(Vector3.new(direction.X * 12, direction.Z * -12 + add, add2))

Solved by converting the vector into object space, then doing whatever i wanted with it such as setting the direction of the velocity up or left, then converted it back to world space for use in hrp.Velocity!

1 Like

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