Example #1
0
    public void BehaviourFlow(List <GameObject> listAgente, List <GameObject> listAlvo, List <GameObject> nodes)
    {
        foreach (GameObject agente in listAgente)
        {
            Steering steering = new Steering();
            for (int i = 0; i < agente.GetComponent <IA_Client>().acao.Length; i++)
            {
                GameObject alvo = listAlvo[0];
                if (listAlvo.Count > 1)
                {
                    for (int j = 0; j < listAlvo.Count - 1; j++)
                    {
                        if (Vector3.Distance(agente.transform.position, listAlvo[j].transform.position) < Vector3.Distance(agente.transform.position, alvo.transform.position))
                        {
                            alvo = listAlvo[j];
                        }
                    }
                }

                switch (agente.GetComponent <IA_Client>().acao[i])
                {
                case Acoes.Flee:
                    steering = sb.Flee(agente, alvo);
                    break;

                case Acoes.Arrive:
                    steering = sb.Arrival(agente, alvo);
                    break;

                case Acoes.Pursue:
                    steering = sb.Pursue(agente, alvo);
                    break;

                case Acoes.Avoid:
                    steering = sb.Avoid(agente, alvo);
                    break;

                case Acoes.AvoidAgent:
                    steering = sb.AvoidAgent(agente, listAgente);
                    break;

                case Acoes.PathFollower:
                    steering = sb.PathFollower(agente, nodes);
                    break;

                case Acoes.Seek:
                default:
                    steering = sb.Seek(agente, alvo);
                    break;
                }

                agente.GetComponent <IA_Client>().steering = steering;
                Debug.DrawRay(agente.transform.position, steering.direcao, Color.red);
                agente.transform.rotation = Quaternion.Slerp(agente.transform.rotation, Quaternion.LookRotation(steering.direcao), Time.deltaTime * agente.GetComponent <IA_Client>().velocidadeRotacao);
                agente.transform.Translate(0, 0, steering.velocidade * Time.deltaTime);
            }
        }
    }
Example #2
0
    // Draw gizmos when this agent is selected in the scene view
    private void OnDrawGizmosSelected()
    {
        if (_steering == null)
        {
            return;
        }
        var currentPosition = transform.position;
        // calculate steering
        Vector3 seek = Vector3.zero, arrival = Vector3.zero, flee = Vector3.zero, hide = Vector3.zero;
        var     alignment         = alignmentFactor * _steering.Alignment(_neighbors);
        var     cohesion          = cohesionFactor * _steering.Cohesion(currentPosition, _neighbors);
        var     separation        = separationFactor * _steering.Separation(currentPosition, _neighbors);
        var     obstacleAvoidance = obstacleAvoidanceFactor * _steering.ObstacleAvoidance(currentPosition, _rigidbody.velocity, _obstacles);
        var     wander            = wanderFactor * _steering.Wander(currentPosition, transform.forward);

        if (_isarrivalTargetNotNull)
        {
            arrival = arrivalFactor * _steering.Arrival(currentPosition, arrivalTarget.position);
        }
        if (_isseekTargetNotNull)
        {
            seek = seekFactor * _steering.Seek(currentPosition, seekTarget.position);
        }
        if (_isfleeTargetNotNull)
        {
            flee = fleeFactor * _steering.Flee(currentPosition, fleeTarget.position);
        }
        if (_ishideTargetNotNull)
        {
            hide = hideFactor * _steering.Hide(currentPosition, hideTarget.position, _obstacles);
        }
        // calculate acceleration (combine steering behaviours and limit acceleration)
        Vector3 acceleration = alignment + cohesion + separation + obstacleAvoidance + wander + arrival + seek + flee + hide;

        if (acceleration.magnitude > maxAcceleration)
        {
            acceleration = acceleration.normalized * maxAcceleration;
        }
        if (acceleration.magnitude < minAcceleration)
        {
            acceleration = acceleration.normalized * minAcceleration;
        }
        // draw vector gizmos
        Gizmos.color = Color.red;
        Gizmos.DrawRay(currentPosition, cohesion);
        Gizmos.color = Color.green;
        Gizmos.DrawRay(currentPosition, separation);
        Gizmos.color = Color.blue;
        Gizmos.DrawRay(currentPosition, alignment);
        Gizmos.color = Color.white;
        Gizmos.DrawRay(currentPosition, obstacleAvoidance);
        Gizmos.color = Color.grey;
        Gizmos.DrawRay(currentPosition, flee);
        Gizmos.color = Color.black;
        Gizmos.DrawRay(currentPosition, hide);
        Gizmos.color = Color.yellow;
        Gizmos.DrawRay(currentPosition, wander);
        Gizmos.DrawRay(currentPosition, arrival);
        Gizmos.DrawRay(currentPosition, seek);
        Gizmos.color = Color.magenta;
        Gizmos.DrawRay(currentPosition, acceleration);
    }