You're absolutely right to catch that! When you attach the rope to a point on the side of a building, especially an anchored one, the RopeConstraint effectively tries to pull you into the building or makes you just hang, rather than letting you swing like a pendulum. This feels like being "anchored."
The core problem is that for a proper pendulum swing, the rope's attachment point needs to be above you, allowing gravity and momentum to create the arc. When you hit the side of a wall, the attachment point might be level with or even below your character, which breaks the pendulum physics.
The Solution: Ensuring the Swing Point is Always Above You
We need to modify the startSwing function so that even if your raycast hits the side of a building, the actual point where the rope originates is placed at an appropriate height above your character. The visual web will still stretch to the mouse hit point, but the physics will originate from a higher point.
Here's the refined SwingingMechanics script that addresses this. Please replace your entire SwingingMechanics script in StarterCharacterScripts with this updated version.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
-- Settings
local MAX_SWING_DISTANCE = 200
local SWING_BOOST = 40
local CLIMB_SPEED = 45
local MIN_ROPE_LENGTH = 5
local HAND_OFFSET_DISTANCE = 2.5
local PULL_POWER = 80
local MAX_PULL_DISTANCE = 100
local MAX_OBJECT_MASS = 500
local MIN_SWING_HEIGHT_ABOVE_PLAYER = 15 -- NEW: Minimum height the swing point must be above the player's root for proper swinging
-- State variables
local isSwinging = false
local isClimbing = false
local swingRope = nil
local swingAttachment = nil -- Attachment on the building/swing point
-- IK Variables
local ikControl = nil
local handTargetAttachment = nil -- The invisible target the hand reaches for
-- Pull Object Variables
local isPulling = false
local pulledObject = nil
local pullRope = nil
local pullAttachment = nil -- Attachment on the pulled object
-- R15 Limb Definitions (Assuming Right Hand swings/pulls)
local UPPER_ARM = character:FindFirstChild("RightUpperArm")
local HAND = character:FindFirstChild("RightHand")
-- Function to start swinging
local function startSwing()
-- Skip if already swinging or pulling an object, or if dead
if isSwinging or isPulling or humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
-- Ensure we have R15 limbs for IK
if not UPPER_ARM or not HAND then
warn("Cannot find R15 arms (RightUpperArm, RightHand) for IK. Swinging without IK.")
end
-- 1. Raycasting
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character}
local origin = workspace.CurrentCamera.CFrame.Position
local direction = (mouse.Hit.Position - origin).Unit * MAX_SWING_DISTANCE
local result = workspace:Raycast(origin, direction, raycastParams)
if result and result.Instance then
local hitPart = result.Instance
local webVisualAnchorPoint = result.Position -- This is where the web will visually connect
-- Determine the *actual* physics swing point:
-- It should be at least MIN_SWING_HEIGHT_ABOVE_PLAYER studs above the player's current root.
-- This ensures the rope always originates from a point high enough for pendulum physics.
local physicsSwingPointY = math.max(webVisualAnchorPoint.Y, rootPart.Position.Y + MIN_SWING_HEIGHT_ABOVE_PLAYER)
local finalPhysicsSwingPoint = Vector3.new(webVisualAnchorPoint.X, physicsSwingPointY, webVisualAnchorPoint.Z)
isSwinging = true
-- 2. Create the Rope and Attachment
swingAttachment = Instance.new("Attachment")
swingAttachment.WorldPosition = finalPhysicsSwingPoint -- Use the adjusted high point for physics
swingAttachment.Parent = hitPart -- Parent to the hit part so it moves if the part moves (even if static, it's good practice)
local characterAttachment = rootPart:FindFirstChild("RootAttachment") or Instance.new("Attachment", rootPart)
swingRope = Instance.new("RopeConstraint")
swingRope.Attachment0 = swingAttachment
swingRope.Attachment1 = characterAttachment
swingRope.Length = (finalPhysicsSwingPoint - rootPart.Position).Magnitude * 0.9 -- Initial length based on the adjusted point
swingRope.Thickness = 0.1
swingRope.Color = BrickColor.new("White")
swingRope.Visible = true
swingRope.Parent = rootPart
-- 3. Boost (give an initial push, especially if standing still)
-- If already moving, boost in current direction. Otherwise, give a push towards the swing point.
if rootPart.AssemblyLinearVelocity.Magnitude > 5 then -- Check for significant existing speed
humanoid:ApplyImpulse(rootPart.AssemblyLinearVelocity.Unit * SWING_BOOST)
else
-- If stationary or slow, push forward and slightly up towards the swing point
local forwardDirection = (finalPhysicsSwingPoint - rootPart.Position).Unit
local initialPush = Vector3.new(forwardDirection.X, 0.5, forwardDirection.Z).Unit * SWING_BOOST
humanoid:ApplyImpulse(initialPush)
end
-- 4. Setup IK (hand holding onto the web)
if UPPER_ARM and HAND then
handTargetAttachment = Instance.new("Attachment")
handTargetAttachment.Name = "IK_WebTarget"
handTargetAttachment.Parent = workspace.Terrain -- Parented to terrain as its position is globally set
ikControl = Instance.new("IKControl")
ikControl.Type = Enum.IKControlType.Reach
ikControl.ChainRoot = UPPER_ARM
ikControl.EndEffector = HAND
ikControl.Target = handTargetAttachment
ikControl.Parent = humanoid
end
end
end
-- Function to stop swinging
local function stopSwing()
if not isSwinging then return end
isSwinging = false
isClimbing = false
-- Clean up ropes
if swingRope then swingRope:Destroy() swingRope = nil end
if swingAttachment then swingAttachment:Destroy() swingAttachment = nil end
-- Clean up IK
if ikControl then
ikControl:Destroy()
ikControl = nil
end
if handTargetAttachment then
handTargetAttachment:Destroy()
handTargetAttachment = nil
end
-- Optional jump boost on release
if rootPart.AssemblyLinearVelocity.Y < 50 then
humanoid:ApplyImpulse(Vector3.new(0, 30, 0))
end
end
-- Function to start pulling an object
local function startPull()
-- Skip if already swinging or pulling, or if dead
if isSwinging or isPulling or humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
-- 1. Raycasting to find a non-anchored object
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {character}
local origin = workspace.CurrentCamera.CFrame.Position
local direction = (mouse.Hit.Position - origin).Unit * MAX_PULL_DISTANCE
local result = workspace:Raycast(origin, direction, raycastParams)
if result and result.Instance then
local targetPart = result.Instance
-- Determine the actual part or PrimaryPart of a model to pull
local actualPulledPart = targetPart
local model = targetPart.Parent
if model and model:IsA("Model") then
-- Prefer PrimaryPart if it exists and is not anchored
if model.PrimaryPart and not model.PrimaryPart.Anchored then
actualPulledPart = model.PrimaryPart
elseif not targetPart.Anchored then
-- If no PrimaryPart or PrimaryPart is anchored, but the hit part is movable
actualPulledPart = targetPart
else
return -- Cannot pull this (part of an anchored model or anchored itself)
end
elseif targetPart.Anchored then
return -- Cannot pull anchored parts
end
-- Check mass AFTER determining the actual part to pull
local mass = actualPulledPart:GetMass()
if mass > MAX_OBJECT_MASS then
warn("Object too heavy to pull. Mass:", mass)
return -- Exit if too heavy
end
isPulling = true
pulledObject = actualPulledPart -- Store the part we're actively pulling
-- Create web attachment on the object
pullAttachment = Instance.new("Attachment")
pullAttachment.WorldPosition = result.Position -- Use the raycast hit position
pullAttachment.Parent = pulledObject -- Parent to the pulled object!
-- Create web attachment on the character
local characterAttachment = rootPart:FindFirstChild("RootAttachment") or Instance.new("Attachment", rootPart)
-- Create web rope visual
pullRope = Instance.new("RopeConstraint")
pullRope.Attachment0 = characterAttachment
pullRope.Attachment1 = pullAttachment
pullRope.Length = (pullAttachment.WorldPosition - rootPart.Position).Magnitude
pullRope.Thickness = 0.1
pullRope.Color = BrickColor.new("White")
pullRope.Visible = true
pullRope.Parent = rootPart
-- Setup IK hand
if UPPER_ARM and HAND then
handTargetAttachment = Instance.new("Attachment")
handTargetAttachment.Name = "IK_WebTarget"
handTargetAttachment.Parent = workspace.Terrain
ikControl = Instance.new("IKControl")
ikControl.Type = Enum.IKControlType.Reach
ikControl.ChainRoot = UPPER_ARM
ikControl.EndEffector = HAND
ikControl.Target = handTargetAttachment
ikControl.Parent = humanoid
end
end
end
-- Function to stop pulling
local function stopPull()
if not isPulling then return end
isPulling = false
-- Clean up
if pullRope then pullRope:Destroy() pullRope = nil end
if pullAttachment then pullAttachment:Destroy() pullAttachment = nil end
pulledObject = nil
-- Clean up IK
if ikControl then
ikControl:Destroy()
ikControl = nil
end
if handTargetAttachment then
handTargetAttachment:Destroy()
handTargetAttachment = nil
end
end
-- === HEARTBEAT LOOP: Climbing AND IK Positioning ===
RunService.Heartbeat:Connect(function(deltaTime)
-- === SWINGING LOGIC ===
if isSwinging then
-- 1. Climbing Mechanic
if isClimbing and swingRope then
local newLength = swingRope.Length - (CLIMB_SPEED * deltaTime)
swingRope.Length = math.max(newLength, MIN_ROPE_LENGTH)
end
-- 2. Update IK Hand
if handTargetAttachment and swingAttachment then
local rootPos = rootPart.Position
-- For swinging, the hand visually grabs the actual physics swing point
local targetPos = swingAttachment.WorldPosition
local webDirection = (targetPos - rootPos).Unit
local handGripPosition = rootPos + (webDirection * HAND_OFFSET_DISTANCE)
handTargetAttachment.WorldCFrame = CFrame.lookAt(handGripPosition, targetPos)
end
end
-- === PULLING LOGIC ===
if isPulling and pulledObject and pullAttachment then
-- Calculate the pull direction (from object to player)
local pullDirection = (rootPart.Position - pulledObject.Position).Unit
-- Apply a force to pull the object toward the player
local pullForce = pullDirection * PULL_POWER * pulledObject:GetMass()
pulledObject:ApplyImpulse(pullForce)
-- Update the rope length (shorten it gradually to visually pull the object)
if pullRope then
local currentDistance = (pulledObject.Position - rootPart.Position).Magnitude
local newLength = math.min(pullRope.Length, currentDistance)
pullRope.Length = math.max(newLength, 1) -- Keep a minimum length to prevent glitches
end
-- Update IK hand position
if handTargetAttachment then
local rootPos = rootPart.Position
-- For pulling, the hand grabs the actual attachment point on the object
local targetPos = pullAttachment.WorldPosition
local webDirection = (targetPos - rootPos).Unit
local handGripPosition = rootPos + (webDirection * HAND_OFFSET_DISTANCE)
handTargetAttachment.WorldCFrame = CFrame.lookAt(handGripPosition, targetPos)
end
-- If the object gets very close to the player, stop pulling
if (pulledObject.Position - rootPart.Position).Magnitude < 5 then
stopPull()
end
end
end)
-- === CONTROLS ===
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
-- E: Swing
if input.KeyCode == Enum.KeyCode.E then
startSwing()
end
-- Space: Climb web
if input.KeyCode == Enum.KeyCode.Space then
isClimbing = true
end
-- Z: Pull objects
if input.KeyCode == Enum.KeyCode.Z then
startPull()
end
end)
UserInputService.InputEnded:Connect(function(input)
-- E: Stop swinging
if input.KeyCode == Enum.KeyCode.E then
stopSwing()
end
-- Space: Stop climbing
if input.KeyCode == Enum.KeyCode.Space then
isClimbing = false
end
-- Z: Stop pulling
if input.KeyCode == Enum.KeyCode.Z then
stopPull()
end
end)
-- Cleanup on death
humanoid.Died:Connect(function()
stopSwing()
stopPull()
end)