Make a Character Reach with SpriteKit Inverse Kinematics
Aim a character’s hand at a switch, ledge, or moving rope while SpriteKit works out the arm’s joint angles.
Start with a two-link arm built from nodes. Prototype with simple shapes, then replace them with separate upper-arm and forearm artwork. A single baked character sprite gives the solver no joints to move.
Build the arm from shoulder to hand
Attach the elbow to the end of the upper arm, then the hand to the end of the forearm. Draw each piece relative to its pivot. Constrain the offsets so the joints can rotate while the link lengths stay fixed.
import SpriteKit
@MainActor
func buildArm(in scene: SKScene) -> (shoulder: SKNode, hand: SKNode) {
let shoulder = SKNode()
let elbow = SKNode()
let hand = SKNode()
scene.addChild(shoulder)
shoulder.position = CGPoint(x: 160, y: 200)
shoulder.addChild(elbow)
elbow.position = CGPoint(x: 0, y: -46)
elbow.addChild(hand)
hand.position = CGPoint(x: 0, y: -38)
let upperArm = SKShapeNode(rect: CGRect(x: -3, y: -46, width: 6, height: 46))
let forearm = SKShapeNode(rect: CGRect(x: -3, y: -38, width: 6, height: 38))
shoulder.addChild(upperArm)
elbow.addChild(forearm)
shoulder.constraints = [.positionX(SKRange(constantValue: 160),
y: SKRange(constantValue: 200))]
elbow.constraints = [.positionX(SKRange(constantValue: 0),
y: SKRange(constantValue: -46))]
hand.constraints = [.positionX(SKRange(constantValue: 0),
y: SKRange(constantValue: -38))]
elbow.reachConstraints = SKReachConstraints(lowerAngleLimit: -.pi / 6,
upperAngleLimit: .pi * 0.8)
return (shoulder, hand)
}
@MainActor
func reach(shoulder: SKNode, hand: SKNode) {
let target = CGPoint(x: 205, y: 155)
let motion = SKAction.reach(to: target, rootNode: shoulder, duration: 0.15)
hand.run(motion, withKey: "reach")
}
Run the action on the hand. The shoulder is the root of the solve. Run the example with a reachable target, then move it farther away and watch where the arm reaches its limit.
Use a moving rope grip as the target
In a climbing game, take the visual target from the current grip and resolve it in the scene’s coordinates. Refresh the keyed reach from the current pose as the target moves. For a player carrying a layered rig, attach the shoulder to the player and fix its offset relative to that parent instead of pinning it to a world position.
Let the game decide whether the grab succeeds. The reach action poses the artwork. That keeps a stretched arm from granting an attachment outside the game’s actual grab radius.
Start with the sample’s reach(shoulder:hand:). Give it a scene-space target parameter in place of the fixed point. If you have a node marking the grip, pass grip.convert(CGPoint.zero, to: scene) as that point. First move the marker between two locations and trigger the keyed reach after each move. For a continuously swinging rope, refresh as the grip moves so the hand follows the latest position.