Make a node automatically rotate itself based on the changing position of another node, by using orientation constraints.
A common use for orientation constraints is to make a look-at constraint. For example, you may create a look-at constraint to make a pair of eyes follow a moving object or to have a rocket point in the direction of its target.
The following code shows how to create a constraint to make a sprite node named
pointer always point toward a circular-shape node named
target. The texture assigned to
pointer is an upward-pointing arrow and, because SpriteKit's angular coordinates have their origin at the three o'clock position, the orient constraint has an offset of 90° (
/2 radians) so that the arrow is correctly oriented toward the target.
A further orientation constraint,
z, can be used in combination with the look-at constraint shown above to limit rotation. When you add the code above, the arrow's rotation is limited so that it never points downward: