Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cache common character parts
- local character = script.Parent
- local humanoid = character:WaitForChild("Humanoid")
- local hrp = character:WaitForChild("HumanoidRootPart")
- local leftFoot = character:WaitForChild("LeftFoot")
- local animateFolder = character:WaitForChild("Animate")
- local climbAnimAsset = animateFolder.climb:WaitForChild("ClimbAnim")
- -- Load the climbing animation
- local climbAnim = humanoid:LoadAnimation(climbAnimAsset)
- -- Create raycast parameters once to avoid recreating them every frame
- local raycastParams = RaycastParams.new()
- raycastParams.FilterDescendantsInstances = {character} -- ignore the character itself
- -- Define constants for climbing behavior
- local CLIMB_FORCE = 20 -- upward velocity to simulate climbing
- local RAYCAST_DISTANCE = 1.3 -- distance to check for a wall
- -- Get RunService to connect to the Heartbeat event
- local runService = game:GetService("RunService")
- runService.Heartbeat:Connect(function()
- -- Define ray origin and direction based on the left foot and the character's look vector
- local origin = leftFoot.Position
- local direction = hrp.CFrame.LookVector * RAYCAST_DISTANCE
- -- Perform the raycast
- local raycastResult = workspace:Raycast(origin, direction, raycastParams)
- if raycastResult and raycastResult.Instance then
- -- Apply upward force while maintaining current horizontal velocity
- hrp.Velocity = Vector3.new(hrp.Velocity.X, CLIMB_FORCE, hrp.Velocity.Z)
- -- Ensure the humanoid state is set to Climbing
- if humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then
- humanoid:ChangeState(Enum.HumanoidStateType.Climbing)
- end
- -- Play the climbing animation if not already playing
- if not climbAnim.IsPlaying then
- climbAnim:Play()
- end
- else
- -- Stop the climbing animation if no wall is detected
- if climbAnim.IsPlaying then
- climbAnim:Stop()
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement