- Home /
Collision Avoidance causes jiggling
Hello! I've got a lot of C# experience but am fairly new to pathing and unity.
My local avoidance code looks like:
public Vector3 LocalAvoidance(Vector3 heading)
{
Vector3 avoid = Vector3.zero;
foreach (var unit in this.teamManager.TeamUnits)
{
if (unit == this || unit.enabled == false) continue;
var dist = Vector3.Distance(unit.transform.position, this.transform.position);
if (dist < radius * 2)
{
avoid += this.transform.position - unit.transform.position;
}
}
heading = heading.normalized + avoid.normalized;
// no modification necessary for flocking behavior
return heading;
}
And my turning code looks something like:
Vector3 heading = GoalPos - transform.position;
Vector3 newHeading = LocalAvoidance(heading);
var newRotation =
Quaternion.RotateTowards(
transform.rotation,
Quaternion.LookRotation(newHeading),
turnSpeed);
transform.rotation = newRotation;
I'm guessing I'm supposed to "remember" what I'm avoiding rather than having a hard distance check.
The avoidance function is influenced by a Boids tutorial I went through the other day, but I'm obviously still missing a few bits.
Thank you for any help!
Answer by medinaline · Dec 18, 2019 at 04:59 PM
Could it be as simple as a bool isAvoiding property on the object, to tell it to behave differently for the number of frames it takes to get past the obstacle it's avoiding? Then you could check isAvoiding before assigning the newRotation. That would keep it from turning into the obstacle every frame before turning away (jiggling).
I'm attempting to avoid some of the more "manual effort" situations where values will have to be routinely tweaked, worry about framerate, or otherwise be non-mathematical in nature.
The root cause is something to do with a lack of understanding regarding avoidance so tweaking it to get a temporary fix isn't ideal I$$anonymous$$O.
Answer by hm5k · Dec 19, 2019 at 01:19 AM
I've found a reasonable approximation of the correct solution, but not quite good enough I'd want to accept this as the answer to my question.
I've added a multiplier that allows me to linearly extend the distance to avoid entering and exiting the avoidance area, code below:
heading = heading.normalized + avoid.normalized * AvoidanceMult(temp);
// no modification necessary for flocking behavior
return heading;
float AvoidanceMult(float dist)
{
return (AVOID_DIST * radius - dist) / radius;
}
AVOID DIST is simply a constant that corresponds to the diff check from before. (in that example it'd be 2, though 3 gives me a linearly interpolated "extra radius" to adjust.)
I'll keep an eye out for if someone posts a better answer, or I'll accept this one in a week as a work around. I believe the correct answer is located in Boids knowledge however.