public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { float rayDist = lookAheadSeconds * mine.Velocity.magnitude; Ray myRay = new Ray(mine.WorldPosition, mine.WorldForward); if (!ObstacleInPath(myRay, mine.shape.radius + clearance, rayDist, ref hit, mask)) { steer = Vector3.zero; mine.SetAgentProperty(lastClearDirectionKey, steer); return; } float hitDist = hit.distance; Vector3 lastClearWorldDirection = Vector3.zero; if (mine.HasAgentProperty(lastClearDirectionKey)) { lastClearWorldDirection = mine.GetAgentProperty <Vector3>(lastClearDirectionKey); } if (lastClearWorldDirection == Vector3.zero) { lastClearWorldDirection = myRay.direction; } myRay.direction = lastClearWorldDirection; steer = ObstacleRays(myRay, mine.shape.radius + clearance, rayDist, ref hit, mask); mine.SetAgentProperty(lastClearDirectionKey, steer.normalized); float smooth = (1f - (hitDist / rayDist)); steer = mine.WorldToFlockBoxDirection(steer); steer = steer.normalized * mine.activeSettings.maxForce - mine.Velocity; steer *= smooth; }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { Vector3 fleeMidpoint = Vector3.zero; float count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { fleeMidpoint += (other.Position); count++; } } if (count > 0) { fleeMidpoint /= (count); mine.GetSteerVector(out steer, (mine.Position - fleeMidpoint)); mine.SetAgentProperty(fleeAttributeName, true); } else { mine.SetAgentProperty(fleeAttributeName, false); steer = Vector3.zero; } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { HashSet <Agent> leaders = GetFilteredAgents(surroundings, this); if (leaders.Count == 0) { mine.RemoveAgentProperty(leaderIDAttributeName); steer = Vector3.zero; return; } Agent closestLeader = SeekBehavior.ClosestPursuableTarget(leaders, mine); mine.SetAgentProperty(leaderIDAttributeName, closestLeader.agentID); //check to see if we should clear the way in front of the leader float scalar = Vector3.Dot(mine.Position - closestLeader.Position, closestLeader.Forward); if (scalar > 0 && scalar < clearAheadDistance)//we are somewhere in front of the leader, potentially in the clear zone ahead of it. { pointOnLeaderPath_cached = closestLeader.Position + closestLeader.Forward * scalar; float insideClearZone = (pointOnLeaderPath_cached - mine.Position).sqrMagnitude / (clearAheadRadius * clearAheadRadius); //0-1 is inside zone, <1 is outside if (insideClearZone <= 1) { steer = (mine.Position - pointOnLeaderPath_cached).normalized * (1f - insideClearZone) * mine.activeSettings.maxForce; return; } } Vector3 desired_velocity = ArriveBehavior.DesiredVelocityForArrival(mine, closestLeader.Position - closestLeader.Forward * followDistance, stoppingRadius); steer = desired_velocity - mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { if (!mine.HasAgentProperty(targetIDAttributeName)) { mine.SetAgentProperty(targetIDAttributeName, -1); } int chosenTargetID = mine.GetAgentProperty <int>(targetIDAttributeName); HashSet <Agent> allTargets = GetFilteredAgents(surroundings, this); if (allTargets.Count == 0) { if (HasPursuitTarget(mine)) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } Agent closestTarget = ClosestPursuableTarget(allTargets, mine); if (!closestTarget || !closestTarget.CanBeCaughtBy(mine)) { if (HasPursuitTarget(mine)) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } if (closestTarget.agentID != chosenTargetID) { DisengagePursuit(mine, chosenTargetID); EngagePursuit(mine, closestTarget); } Vector3 distance = closestTarget.Position - mine.Position; float est_timeToIntercept = distance.magnitude / mine.activeSettings.maxSpeed; Vector3 predictedInterceptPosition = closestTarget.Position + closestTarget.Velocity * est_timeToIntercept; AttemptCatch(mine, closestTarget); mine.GetSeekVector(out steer, predictedInterceptPosition); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { if (!mine.HasAgentProperty(targetIDAttributeName)) { mine.SetAgentProperty(targetIDAttributeName, -1); } int chosenTargetID = mine.GetAgentProperty <int>(targetIDAttributeName); HashSet <Agent> allTargets = GetFilteredAgents(surroundings, this); if (allTargets.Count == 0) { if (chosenTargetID != -1) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } Agent closestTarget = ClosestPursuableTarget(allTargets, mine); if (!closestTarget || !closestTarget.CanBeCaughtBy(mine)) { if (chosenTargetID != -1) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } if (closestTarget.agentID != chosenTargetID) { DisengagePursuit(mine, chosenTargetID); EngagePursuit(mine, closestTarget); } AttemptCatch(mine, closestTarget); Vector3 desired_velocity = (closestTarget.Position - mine.Position).normalized * mine.activeSettings.maxSpeed; steer = desired_velocity - mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); }
protected static void DisengagePursuit(SteeringAgent mine, int targetID) { mine.SetAgentProperty(targetIDAttributeName, -1); }
protected static void EngagePursuit(SteeringAgent mine, Agent target) { mine.SetAgentProperty(targetIDAttributeName, target.agentID); }