Initializer

init(toAlignWith:maxDistance:maxAngle:)

Creates a goal whose effect is to make an agent align its orientation with that of other agents in a specified group.

Declaration

convenience init(toAlignWith agents: [GKAgent], maxDistance: Float, maxAngle: Float)

Parameters

agents

The agents with whom to maintain alignment.

maxDistance

The maximum distance from other agents required for this goal to take effect.

maxAngle

The maximum angle, in radians, between an affected agent’s velocity and the direction toward the other agents required for this goal to take effect.

Return Value

A new goal object.

Discussion

The agents array can safely include the agent(s) affected by the goal—an agent pursuing this goal will ignore itself in the array. Therefore, you can use a single goal created with this method to cause an entire group of agents to mutually align with one another.

Changing the maxDistance parameter determines the minimum distance between agents in the group. Changing the maxAngle parameter determines how tightly an agent will turn to maintain alignment with the group.

You can combine separation, alignment, and cohesion goals to produce “flocking” behaviors in which a group of agents move together.

See Also

Creating Goals for Flocking Behavior

init(toSeparateFrom: [GKAgent], maxDistance: Float, maxAngle: Float)

Creates a goal whose effect is to make an agent maintain the specified distance from other agents in a specified group.

init(toCohereWith: [GKAgent], maxDistance: Float, maxAngle: Float)

Creates a goal whose effect is to make an agent stay near the other agents in a specified group.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software