<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SpriteKit",
  "identifier" : "/documentation/SpriteKit/SKConstraint",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "SpriteKit"
    ],
    "preciseIdentifier" : "c:objc(cs)SKConstraint"
  },
  "title" : "SKConstraint"
}
-->

# SKConstraint

A specification for constraining a node’s position or rotation.

```
class SKConstraint
```

## Overview

An [`SKConstraint`](/documentation/SpriteKit/SKConstraint) object describes a mathematical constraint on a node’s position or orientation. You attach constraints to nodes; after a scene processes any actions and physics interactions, it applies constraints attached to nodes in its node tree. Use constraints to ensure that certain relationships are true before the system renders a scene. For example, you might use a constraint to:

- Change a node’s [`zRotation`](/documentation/SpriteKit/SKNode/zRotation) property so that it always points at another node or a position in the scene.
- Keep a node within a specified distance of another node or a point in the scene.
- Keep a node inside a specified rectangle.
- Restrict the [`zRotation`](/documentation/SpriteKit/SKNode/zRotation) property of a node so that it has a more limited rotation range of motion.

To use constraints, create an <doc://com.apple.documentation/documentation/Foundation/NSArray> object that contains one or more constraint objects and assign the array to a node’s [`constraints`](/documentation/SpriteKit/SKNode/constraints) property. When the system evaluates a scene, it executes the constraints on a node in the order they appear in the [`constraints`](/documentation/SpriteKit/SKNode/constraints) array.

You can’t change a constraint after you create it. However, you can selectively disable or enable a constraint by setting its [`enabled`](/documentation/SpriteKit/SKConstraint/enabled) property. You can also use the [`referenceNode`](/documentation/SpriteKit/SKConstraint/referenceNode) property to convert positions to the referenced coordinate system before applying the constraint.

## Topics

### Creating Position Constraints

Limit a node’s movement in one axis or both.

[Creating Position Constraints](/documentation/SpriteKit/creating-position-constraints)

Create a position constraint and add it to a node.

[`positionX(_:y:)`](/documentation/SpriteKit/SKConstraint/positionX(_:y:))

Creates a constraint that restricts both coordinates of a node’s position.

[`positionX(_:)`](/documentation/SpriteKit/SKConstraint/positionX(_:))

Creates a constraint that restricts the x-coordinate of a node’s position.

[`positionY(_:)`](/documentation/SpriteKit/SKConstraint/positionY(_:))

Creates a constraint that restricts the y-coordinate of a node’s position.

### Creating Orientation Constraints

Provide limitations on a node’s rotation, or create rules the system uses to update a node’s rotation for you.

[Creating a Look-At Constraint](/documentation/SpriteKit/creating-a-look-at-constraint)

Make a node automatically rotate itself based on the changing position of another node, by using orientation constraints.

[`orient(to:offset:)`](/documentation/SpriteKit/SKConstraint/orient(to:offset:)-1h1tw)

Creates a constraint that forces a node to rotate to face another node.

[`orient(to:offset:)`](/documentation/SpriteKit/SKConstraint/orient(to:offset:)-9lq3h)

Creates a constraint that forces a node to rotate to face a fixed point.

[`orient(to:in:offset:)`](/documentation/SpriteKit/SKConstraint/orient(to:in:offset:))

Creates a constraint that forces a node to rotate to face a point in another node’s coordinate system.

[`zRotation(_:)`](/documentation/SpriteKit/SKConstraint/zRotation(_:))

Creates a constraint that limits the orientation of a node.

### Creating Distance Constraints

Create a rule the system uses to update a node’s position relative to another node in the scene.

[`distance(_:to:)`](/documentation/SpriteKit/SKConstraint/distance(_:to:)-6507j)

Creates a constraint that keeps a node within a certain distance of another node.

[`distance(_:to:)`](/documentation/SpriteKit/SKConstraint/distance(_:to:)-7yk7n)

Creates a constraint that keeps a node within a certain distance of a point.

[`distance(_:to:in:)`](/documentation/SpriteKit/SKConstraint/distance(_:to:in:))

Creates a constraint that keeps a node within a certain distance of a point in another node’s coordinate system.

### Controlling the Coordinate System Where a Constraint is Applied

[`referenceNode`](/documentation/SpriteKit/SKConstraint/referenceNode)

The node whose coordinate system should be used to apply the constraint.

### Enabling and Disabling a Constraint

[`enabled`](/documentation/SpriteKit/SKConstraint/enabled)

A Boolean value that specifies whether the constraint is applied.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)