예제 #1
0
        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;
        }
예제 #2
0
        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"));
            }
        }
예제 #3
0
 public static bool HasPursuitTarget(SteeringAgent mine)
 {
     if (!mine.HasAgentProperty(targetIDAttributeName))
     {
         return(false);
     }
     return(mine.GetAgentProperty <int>(targetIDAttributeName) >= 0);
 }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }