Advertisement
luchitasin9

WallClimbScript

Mar 12th, 2025
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | Source Code | 0 0
  1. -- Cache common character parts
  2. local character = script.Parent
  3. local humanoid = character:WaitForChild("Humanoid")
  4. local hrp = character:WaitForChild("HumanoidRootPart")
  5. local leftFoot = character:WaitForChild("LeftFoot")
  6. local animateFolder = character:WaitForChild("Animate")
  7. local climbAnimAsset = animateFolder.climb:WaitForChild("ClimbAnim")
  8.  
  9. -- Load the climbing animation
  10. local climbAnim = humanoid:LoadAnimation(climbAnimAsset)
  11.  
  12. -- Create raycast parameters once to avoid recreating them every frame
  13. local raycastParams = RaycastParams.new()
  14. raycastParams.FilterDescendantsInstances = {character}  -- ignore the character itself
  15.  
  16. -- Define constants for climbing behavior
  17. local CLIMB_FORCE = 20        -- upward velocity to simulate climbing
  18. local RAYCAST_DISTANCE = 1.3   -- distance to check for a wall
  19.  
  20. -- Get RunService to connect to the Heartbeat event
  21. local runService = game:GetService("RunService")
  22.  
  23. runService.Heartbeat:Connect(function()
  24.     -- Define ray origin and direction based on the left foot and the character's look vector
  25.     local origin = leftFoot.Position
  26.     local direction = hrp.CFrame.LookVector * RAYCAST_DISTANCE
  27.  
  28.     -- Perform the raycast
  29.     local raycastResult = workspace:Raycast(origin, direction, raycastParams)
  30.  
  31.     if raycastResult and raycastResult.Instance then
  32.         -- Apply upward force while maintaining current horizontal velocity
  33.         hrp.Velocity = Vector3.new(hrp.Velocity.X, CLIMB_FORCE, hrp.Velocity.Z)
  34.  
  35.         -- Ensure the humanoid state is set to Climbing
  36.         if humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then
  37.             humanoid:ChangeState(Enum.HumanoidStateType.Climbing)
  38.         end
  39.  
  40.         -- Play the climbing animation if not already playing
  41.         if not climbAnim.IsPlaying then
  42.             climbAnim:Play()
  43.         end
  44.     else
  45.         -- Stop the climbing animation if no wall is detected
  46.         if climbAnim.IsPlaying then
  47.             climbAnim:Stop()
  48.         end
  49.     end
  50. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement