Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min/max cohesion contribution for more controllable flocking #50

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions 2D/Behaviors/SteerForCohesion2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ namespace UnitySteer2D.Behaviors
[RequireComponent(typeof (SteerForNeighborGroup2D))]
public class SteerForCohesion2D : SteerForNeighbors2D
{
[SerializeField] private float _minContribution = 0f;
[SerializeField] private float _maxContribution = 1f;

public override Vector2 CalculateNeighborContribution(Vehicle2D other)
{
// accumulate sum of forces leading us towards neighbor's positions
var distance = other.Position - Vehicle.Position;
var sqrMag = distance.sqrMagnitude;
// Provide some contribution, but diminished by the distance to
// Provide some contribution, but diminished by the distance to
// the vehicle.
distance *= 1 / sqrMag;
distance *= Mathf.Clamp(1 / sqrMag, _minContribution, _maxContribution);
return distance;
}

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# UnitySteer changelog

## WIP

* SteerForCohesion2D now has min/max contribution properties; this can assist
flocking by preventing the cohesion factor from becoming too strong/weak with
distance

## v3.1

* 2D support thanks to @GandaG and @pjohalloran.
Expand Down