protected override void DrawForecastPerceptionGizmo(SteeringAgent agent, float distance) { DrawCylinderGizmo(agent.shape.radius + clearance, distance); Handles.DrawWireDisc(Vector3.forward * distance, Vector3.forward, agent.shape.radius); Handles.Label(Vector3.forward * distance + Vector3.up * agent.shape.radius, new GUIContent("Agent Radius")); Handles.Label(Vector3.forward * distance + Vector3.up * (agent.shape.radius + clearance), new GUIContent("Clearance")); }
public override void DrawPropertyGizmos(SteeringAgent agent, bool drawLabels) { base.DrawPropertyGizmos(agent, drawLabels); Color areaFill = debugColor; areaFill.a *= .1f; if (agent.HasAgentProperty(leaderIDAttributeName)) { int leaderId = agent.GetAgentProperty <int>(leaderIDAttributeName); Agent leader = Agent.GetAgentById(leaderId); if (leader != null) { Handles.matrix = leader.transform.localToWorldMatrix; Gizmos.matrix = leader.transform.localToWorldMatrix; areaFill = Color.clear; } } DrawCylinderGizmo(clearAheadRadius, clearAheadDistance); Handles.DrawLine(Vector3.zero, Vector3.back * followDistance); Gizmos.DrawWireSphere(Vector3.back * followDistance, stoppingRadius); if (drawLabels) { Handles.Label(Vector3.forward * clearAheadDistance, new GUIContent("Clear Ahead Distance")); Handles.Label(Vector3.forward * clearAheadDistance + Vector3.up * clearAheadRadius, new GUIContent("Clear Ahead Radius")); Handles.Label(Vector3.back * followDistance, new GUIContent("Follow Distance")); Handles.Label(Vector3.back * followDistance + Vector3.up * stoppingRadius, new GUIContent("Stopping Radius")); } }
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); }
protected static void AttemptCatch(SteeringAgent mine, Agent target) { if (mine.Overlaps(target)) { mine.CatchAgent(target); } }
public static bool HasPursuitTarget(SteeringAgent mine) { if (!mine.HasAgentProperty(targetIDAttributeName)) { return(false); } return(mine.GetAgentProperty <int>(targetIDAttributeName) >= 0); }
public void AddPerceptions(SteeringAgent agent, SurroundingsContainer surroundings) { surroundings.Clear(); for (int i = 0; i < behaviors.Length; i++) { behaviors[i].AddPerception(agent, surroundings); } }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); foreach (string tag in filterTags) { surroundings.AddGlobalSearchTag(tag); } }
protected bool WithinEffectiveRadius(SteeringAgent mine, Agent other) { if (mine == other) { return(false); } return( Vector3.SqrMagnitude((mine.Position + (mine.Forward * queueDistance)) - other.Position) < queueRadius * queueRadius); // inside fov }
public void DrawPropertyGizmos(SteeringAgent agent) { foreach (SteeringBehavior behavior in behaviors) { if (behavior.DrawProperties) { behavior.DrawPropertyGizmos(agent, !Application.isPlaying); } } }
protected bool WithinEffectiveRadius(SteeringAgent mine, Agent other) { if (mine == other) { return(false); } return( Vector3.SqrMagnitude(mine.Position - other.Position) < effectiveRadius * effectiveRadius && //inside radius Vector3.Angle(mine.Forward, other.Position - mine.Position) <= fieldOfView); // inside fov }
public void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, Vector3 worldDimensions, float containmentBuffer) { bufferedPosition = mine.Position + mine.Velocity * lookAheadSeconds; float distanceToBorder = float.MaxValue; if (worldDimensions.x > 0) { minArray[0] = distanceToBorder; minArray[1] = mine.Position.x; minArray[2] = worldDimensions.x - mine.Position.x; distanceToBorder = Mathf.Min(minArray); bufferedPosition.x = Mathf.Clamp(bufferedPosition.x, containmentBuffer, worldDimensions.x - containmentBuffer); } else { bufferedPosition.x = 0; } if (worldDimensions.y > 0) { minArray[0] = distanceToBorder; minArray[1] = mine.Position.y; minArray[2] = worldDimensions.y - mine.Position.y; distanceToBorder = Mathf.Min(minArray); bufferedPosition.y = Mathf.Clamp(bufferedPosition.y, containmentBuffer, worldDimensions.y - containmentBuffer); } else { bufferedPosition.y = 0; } if (worldDimensions.z > 0) { minArray[0] = distanceToBorder; minArray[1] = mine.Position.z; minArray[2] = worldDimensions.z - mine.Position.z; distanceToBorder = Mathf.Min(minArray); bufferedPosition.z = Mathf.Clamp(bufferedPosition.z, containmentBuffer, worldDimensions.z - containmentBuffer); } else { bufferedPosition.z = 0; } if (bufferedPosition == mine.Position + mine.Velocity) { steer = Vector3.zero; return; } if (distanceToBorder <= 0) { distanceToBorder = .001f; } mine.GetSeekVector(out steer, bufferedPosition); steer *= containmentBuffer / distanceToBorder; }
public override void DrawPropertyGizmos(SteeringAgent agent, bool drawLabels) { base.DrawPropertyGizmos(agent, drawLabels); Handles.color = debugColor; Handles.DrawWireDisc(Vector3.zero, Vector3.up, stoppingDistance); if (drawLabels) { Handles.Label(Vector3.forward * stoppingDistance, new GUIContent("Stopping Distance")); } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { float uniqueId = mine.gameObject.GetInstanceID() * .001f; uniqueId = uniqueId * uniqueId; steer = Quaternion.Euler( (Mathf.PerlinNoise((Time.time * wanderIntensity), uniqueId) - .5f) * wanderScope, (Mathf.PerlinNoise((Time.time * wanderIntensity) + uniqueId, uniqueId) - .5f) * wanderScope, (Mathf.PerlinNoise((Time.time * wanderIntensity) + uniqueId * 2, uniqueId) - .5f) * wanderScope ) * mine.Forward * mine.activeSettings.maxForce; }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { foreach (Agent a in GetFilteredAgents(surroundings, this)) { //another agent is ahead if (WithinEffectiveRadius(mine, a)) { //use brake force steer = -mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); return; } } steer = Vector3.zero; }
public override void DrawPropertyGizmos(SteeringAgent agent, bool drawLabels) { base.DrawPropertyGizmos(agent, drawLabels); float distance = agent.activeSettings.maxSpeed * lookAheadSeconds; if (Application.isPlaying) { distance = agent.Velocity.magnitude * lookAheadSeconds; } DrawForecastPerceptionGizmo(agent, distance); if (drawLabels) { Handles.Label(Vector3.forward * distance, new GUIContent("Look Ahead")); } }
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); }
public override void DrawPropertyGizmos(SteeringAgent agent, bool drawLabels) { base.DrawPropertyGizmos(agent, drawLabels); Handles.DrawWireDisc(Vector3.forward * queueDistance, Vector3.up, queueRadius); Gizmos.DrawWireSphere(Vector3.forward * queueDistance, queueRadius); Handles.DrawLine(Vector3.zero, Vector3.forward * queueDistance); Color c = debugColor; c.a = .1f * c.a; Handles.color = c; Handles.DrawSolidDisc(Vector3.forward * queueDistance, Vector3.up, queueRadius); if (drawLabels) { Handles.Label(Vector3.forward * queueDistance, new GUIContent("Queue Distance")); Handles.Label(Vector3.forward * (queueDistance + queueRadius), new GUIContent("Queue Radius")); } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { HashSet <Agent> obstacles = GetFilteredAgents(surroundings, this); if (obstacles.Count == 0) { steer = Vector3.zero; return; } Ray myRay = new Ray(mine.Position, mine.Forward); float rayDist = surroundings.lookAheadSeconds * mine.Velocity.magnitude; bool foundObstacleInPath = false; foreach (Agent obstacle in obstacles) { if (obstacle.RaycastToShape(myRay, mine.shape.radius + clearance, rayDist, out hit)) { if (!foundObstacleInPath || hit.distance < closestHit.distance) { closestHit = hit; mostImmediateObstacle = obstacle; } foundObstacleInPath = true; } } if (!foundObstacleInPath) { steer = Vector3.zero; return; } mostImmediateObstacle.FindNormalToSteerAwayFromShape(myRay, closestHit, mine.shape.radius, ref normal); steer = normal; steer = steer.normalized * mine.activeSettings.maxForce; steer *= (1f - (closestHit.distance / rayDist)); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { Vector3 sum = Vector3.zero; int count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { float modFactor = 1; sum += (other.Velocity) * modFactor; count++; } } if (count > 0) { sum /= ((float)count); mine.GetSteerVector(out steer, sum); } else { steer = Vector3.zero; } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { //steer used as midpoint to prevent garbage steer = Vector3.zero; float count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { steer += (other.Position); count++; } } if (count > 0) { steer /= (count); mine.GetSeekVector(out steer, steer); } else { steer = Vector3.zero; } }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); surroundings.SetMinPerceptionRadius(effectiveRadius); }
protected static void EngagePursuit(SteeringAgent mine, Agent target) { mine.SetAgentProperty(targetIDAttributeName, target.agentID); }
public virtual void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { }
#pragma warning restore 0414 public abstract void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings);
protected static void DisengagePursuit(SteeringAgent mine, int targetID) { mine.SetAgentProperty(targetIDAttributeName, -1); }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); surroundings.SetMinLookAheadSeconds(lookAheadSeconds); }
protected virtual void DrawForecastPerceptionGizmo(SteeringAgent agent, float distance) { Handles.DrawLine(Vector3.zero, Vector3.forward * distance); }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); Perception.radius = queueRadius; surroundings.AddPerceptionShape(Perception, (agent.Position + (agent.Forward * queueDistance))); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { steer = Vector3.zero; }