Beispiel #1
0
        public override Vector2 SteerForce()
        {
            if (path.Size == 0)
            {
                _bUsingArrive = false;
                return(Vector2.zero);
            }

            else if (path.Size == 1)
            {
                _bUsingArrive       = true;
                arriveSensor.target = path.FirstWaypoint;
                Arrive2D.SteerForce(Agent, arriveSensor);
            }

            if (!path.Finished())
            {
                _bUsingArrive = false;
                return(Seek2D.SteerForce(path.CurrentWaypoint(), Agent));
            }

            else
            {
                _bUsingArrive       = true;
                arriveSensor.target = path.CurrentWaypoint();
                return(Arrive2D.SteerForce(Agent, arriveSensor));
            }
        }
Beispiel #2
0
 void OnDrawGizmosSelected()
 {
     if (target != null)
     {
         Seek2D.DrawGizmos(target.position, Agent);
     }
 }
Beispiel #3
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, Collider2D[] colliders, int totalCount, float detectionRadius)
        {
            Vector2 positionsSum  = Vector2.zero;
            Vector2 steering      = Vector2.zero;
            Vector2 agentPosition = agent.transform.position;

            int   count    = 0;
            float radiusSq = detectionRadius * detectionRadius;

            for (int i = 0; i < totalCount; ++i)
            {
                var other = colliders[i].GetComponent <AutonomousAgent2D>();

                if (other == agent)
                {
                    continue;
                }

                if (agentPosition.WithinDistanceSq(other.transform.position, radiusSq))
                {
                    count        += 1;
                    positionsSum += (Vector2)other.transform.position;
                }
            }

            if (count > 0)
            {
                // The center "mass".
                Vector2 averagePosition = positionsSum / count;
                return(Seek2D.SteerForce(averagePosition, agent));
            }

            return(Vector2.zero);
        }
Beispiel #4
0
 void OnDrawGizmosSelected()
 {
     if (targetRigid)
     {
         Seek2D.DrawGizmos(_futureSeekCache, Agent);
         DrawUtil.DrawLine(_futureSeekCache, targetRigid.position, Color.green);
     }
 }
Beispiel #5
0
        public override Vector2 SteerForce()
        {
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            mousePosition += Random.insideUnitCircle.normalized * randomDisplacement;

            return(Seek2D.SteerForce(mousePosition, Agent));
        }
Beispiel #6
0
        public override Vector2 SteerForce()
        {
            if (target != null)
            {
                return(Seek2D.SteerForce(target.position, Agent));
            }

            return(Vector2.zero);
        }
Beispiel #7
0
        void OnDrawGizmosSelected()
        {
            if (path.waypoints != null && path.Size != 0)
            {
                path.DrawGizmos();

                // Draw the arrival towards the last end point (if no looping)
                if (_bUsingArrive)
                {
                    Arrive2D.DrawGizmos(Agent, arriveSensor);
                }

                // Show which current waypoint we are traveling to.
                else
                {
                    Seek2D.DrawGizmos(path.CurrentWaypoint(), Agent);
                }
            }
        }
Beispiel #8
0
        public override Vector2 SteerForce()
        {
            Vector2 toTarget          = targetRigid.position - (Vector2)Agent.transform.position;
            Vector2 targetVelocityDir = targetRigid.velocity.normalized;

            float relativeForward = Vector2.Dot(Agent.ForwardDirection, targetVelocityDir);

            // If the target is head on, then just seek towards it.
            if (Vector2.Dot(toTarget, Agent.ForwardDirection) > 0 &&
                relativeForward < -0.95f)
            {
                _futureSeekCache = targetRigid.position;

                return(Seek2D.SteerForce(targetRigid.position, Agent));
            }

            float lookAheadTime = toTarget.magnitude / (Agent.maxSpeed + targetRigid.velocity.magnitude);

            Vector2 future = targetRigid.position + targetRigid.velocity * lookAheadTime;

            _futureSeekCache = future;

            return(Seek2D.SteerForce(future, Agent));
        }
Beispiel #9
0
 public override Vector2 SteerForce()
 {
     return(Seek2D.SteerForce(_wanderTarget, Agent));
 }