public Vector3 arrive(BaseAgent agent, Vector3 targetPosition, float arriveRange, float range = 999999)
        {
            Vector3 desired;
            Vector3 difference = targetPosition - agent.transform.position;
            float   distance   = difference.magnitude;

            // rango de funcionamiento general
            if (distance < range)
            {
                // rango de arrive
                if (distance > arriveRange)
                {
                    desired = difference.normalized * agent.maxForce;
                }
                else
                {
                    desired = difference * agent.maxForce;
                }
            }
            else
            {
                desired = Vector3.zero;
            }

            return(desired);
        }
Exemple #2
0
        static public Vector3 Seek(BaseAgent agent, Vector3 targetPosition, float arriveRange = 1)
        {
            Vector3 desiredVector = targetPosition - agent.transform.position;

            desiredVector  = Arrive(desiredVector, arriveRange);
            desiredVector *= agent.maxForce;
            return(desiredVector);
        }
Exemple #3
0
        static public Vector3 Wander2D(BaseAgent agent, float wanderDistance, float wanderRadius, float angleDelta, float maxAngle)
        {
            Vector3 wanderCenter  = agent.velocity.normalized * wanderDistance /* + agent.transform.position*/;
            Vector3 desiredRef    = agent.velocity.normalized * wanderRadius;
            Vector3 desiredVector = wanderCenter + rotateVector2D(desiredRef, Mathf.Clamp(agent.wanderAngle + Random.Range(-angleDelta, angleDelta), -maxAngle, maxAngle)) /* - agent.transform.position*/;

            return(desiredVector.normalized * agent.maxForce);
        }
Exemple #4
0
        static public Vector3 Random2D(BaseAgent agent, Vector3 referencePosition, float changeTargetCD, float randomRadius, float power)
        {
            agent.randomTimer += Time.deltaTime;
            if (agent.randomTimer > changeTargetCD)
            {
                agent.randomTarget = (Vector3)Random.insideUnitCircle * randomRadius + referencePosition;
                agent.randomTimer  = 0;
            }

            return(Seek(agent, agent.randomTarget, power));
        }
        public Vector3 random(BaseAgent agent, Vector3 position, float radius, float frequency)
        {
            timer += Time.deltaTime;

            if (timer >= frequency)
            {
                timer = 0;
                randomTargetPosition = (Vector2)position + Random.insideUnitCircle * Random.Range(0, radius);
            }

            return(seek(agent, randomTargetPosition));
        }
        public Vector3 wander(BaseAgent agent, float wanderDistance, float wanderRadius)
        {
            float   wanderAngle = Random.Range(-180, 180);
            Vector3 center      = agent.transform.position + agent.velocity.normalized * wanderDistance;
            Vector3 wRef        = agent.velocity.normalized * wanderRadius;
            Vector3 wRefRotated = Quaternion.Euler(0, 0, wanderAngle) * wRef;
            Vector3 d           = center + wRefRotated - agent.transform.position;
            Vector3 desired     = (d - agent.transform.position).normalized * agent.maxForce;

            Debug.DrawLine(agent.transform.position, d + agent.transform.position);

            return(desired);
        }
Exemple #7
0
        static public Vector3 Flee(BaseAgent agent, Vector3 targetPosition, float range)
        {
            Vector3 desiredVector;

            if (Vector3.Distance(targetPosition, agent.transform.position) < range)
            {
                desiredVector = agent.transform.position - targetPosition;
                return(desiredVector.normalized * agent.maxForce);
            }
            else
            {
                return(Vector3.zero);
            }
        }
        public Vector3 seek(BaseAgent agent, Vector3 targetPosition, float range = 999999)
        {
            Vector3 desired;
            Vector3 difference = targetPosition - agent.transform.position;

            if (difference.magnitude < range)
            {
                desired = difference.normalized * agent.maxForce;
            }
            else
            {
                desired = Vector3.zero;
            }

            return(desired);
        }
        public Vector3 separate(BaseAgent agent, List <Transform> neighbors, float range)
        {
            Vector3 desired = Vector3.zero;

            foreach (var neighbor in neighbors)
            {
                Vector3 difference = agent.transform.position - neighbor.position;
                if (difference.magnitude > range)
                {
                    continue;
                }

                Vector3 desiredAux = difference.normalized * agent.maxForce;
                desired += desiredAux;
            }

            return(desired);
        }
Exemple #10
0
        static public Vector3 Separate(BaseAgent agent, List <BaseAgent> neighbors, float range)
        {
            Vector3 desiredVector = Vector3.zero;

            for (int i = 0; i < neighbors.Count; i++)
            {
                if (neighbors[i].gameObject != agent.gameObject)
                {
                    desiredVector += Flee(agent, neighbors[i].transform.position, range);
                }
            }
            if (desiredVector.magnitude > 0)
            {
                return(desiredVector.normalized * agent.maxForce);
            }
            else
            {
                return(Vector3.zero);
            }
        }
        // public bool isFirstWanderIteration = true;

        public Vector3 wander(BaseAgent agent, float wanderDistance, float wanderRadius)
        {
            // if (isFirstWanderIteration)
            // {
            // agent.velocity = Random.insideUnitCircle * agent.maxForce;
            // isFirstWanderIteration = false;
            // }

            float   wanderAngle = Random.Range(-180, 180);
            Vector3 center      = agent.transform.position + agent.velocity.normalized * wanderDistance;
            Vector3 wRef        = agent.velocity.normalized * wanderRadius;
            Vector3 wRefRotated = Quaternion.Euler(0, 0, wanderAngle) * wRef;
            Vector3 d           = center + wRefRotated - agent.transform.position;

            Vector3 desired = (d - agent.transform.position).normalized * agent.maxForce;

            Debug.DrawLine(agent.transform.position, d + agent.transform.position);

            return(desired);
        }
Exemple #12
0
        static public Vector3 Cohesion(BaseAgent agent, List <BaseAgent> neighbors, float range)
        {
            Vector3 averagePos = Vector3.zero;
            int     count      = 0;

            for (int i = 0; i < neighbors.Count; i++)
            {
                float dist = Vector3.Distance(agent.transform.position, neighbors[i].transform.position);
                if (dist > 0 && dist < range)
                {
                    averagePos += neighbors[i].transform.position;
                    count++;
                }
            }
            if (count > 0)
            {
                averagePos /= count;
                return(Seek(agent, averagePos));
            }
            return(Vector3.zero);
        }
Exemple #13
0
        static public Vector3 Align(BaseAgent agent, List <BaseAgent> neighbors, float range)
        {
            Vector3 desiredVector = Vector3.zero;
            int     count         = 0;

            for (int i = 0; i < neighbors.Count; i++)
            {
                float dist = Vector3.Distance(agent.transform.position, neighbors[i].transform.position);
                if (dist > 0 && dist < range)
                {
                    desiredVector += neighbors[i].velocity;
                    count++;
                }
            }
            if (count > 0)
            {
                return(desiredVector.normalized * agent.maxForce);
            }
            else
            {
                return(Vector3.zero);
            }
        }